Disclaimer: I am not looking for a full answer to this lab, only hints. I know people get frustrated when others ask to do their homework for them. ;P

Hey guys - first post here, although I have used this website many times to assist me with my Programming classes. I am in an entry level class, so this isn't anything too complicated, I could just use a hint to push me in the right direction.

1. Gets a string from a user.
2. Creates a new string which consists of the characters from the input string, with an asterisk (*) after each character. That is, if the input string was “Hello”, the new string would be “H*e*l*l*o*”. Hint: Think about string concatenation and accumulators. Recall how numeric counters and accumulators work with a loop and extend this to string concatenation.
3. Creates another new string by reversing the characters in the input string using a for loop. That is, if the input string was “Hello”, this new string would be “olleH”. Hint: Remember that string concatenation is not commutative.
4. Prints both new strings, along with appropriate literals.

This is the lab directions given to use by the Professor. I ams struggling a little bit on strings.

This is what I have so far:

inString = raw_input("Please enter a string")
myString = ""
for ch in inString:
    myString = myString + ch
print myString

Of course, this code just prints the data that the user entered. Any hints to put me in the right direction would be very much appreciated.

And just an FYI: The Professor said the code could be done in very few lines (15 or less) so keep that in mind. Thanks in advance everyone.

Recommended Answers

All 6 Replies

You were thinking in the right direction, you just weren't actually doing anything: I'll give you heavy help on one of them and the idea should become quite clear for all of them.

>>> word=input('word: ')
word: hello
>>> word3=''
>>> for letter in word[::-1]:    #which could also be "for letter in reversed(word):"
	word3+=letter
>>>word3
'olleh'

should be able to do it in 9 lines or less.

Also, tell your professor it's far enough into the semester that you should be farther along than string concatenation by now...I learned the majority of what I know in about a month and a half, of course I wasn't stuck in a class for it. Good luck!

start quote:

>>> word3=''
>>> for letter in word[::-1]:    #which could also be "for letter in reversed(word):"
    word3+=letter
>>>word3
'olleh'

end quote.

This does not make much sence as you are building another copy of word[::-1], which is allready the answer:

word = 'Hello'
print word[::-1]

Professor probably wants accumulating the reverse in loop over unreversed word.

for the other part of your question:

word = raw_input('word: ')
word.split()
print '*'.join(word)+'*'

#>>> H*E*L*L*O*

Yes that probably wasn't the best example to use, I wanted to show him how to do what he was trying to do with his for loop, but yes bigredaltoid pytony is quite right [::-1] makes a reversed copy.

Tony, you used to teach python, since reversed(word) isn't really a copy of the word reversed, that would likely work for his professor, correct?

And just an FYI: The Professor said the code could be done in very few lines (15 or less) so keep that in mind.

He was nice with 15 lines you can du a lot in python.

>>> ''.join(i + '*' for i in raw_input('word: '))
word: Hello
'H*e*l*l*o*'
>>> raw_input('word: ')[::-1]
word: Hello
'olleH'

You should try to break this up little,or Professor will understand that you got help.
Something like the code MS posted.

Interactive version:

>>> word = input('Give word: ')
Give word: Hello
>>> stars =  ''.join(c+'*' for c in word)
>>> rev_word = word[::-1]
>>> print 'With stars: %s, reversed: %s' % (stars, rev_word)
With stars: H*e*l*l*o*, reversed: olleH
>>>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.