質問

Let greeting = 'Hello, world!'

(1) Use slicing to change the the letter o to captital O. Notice there are two 'o's!
Save the new string into the variable new_greeting and print it

(2) Instead of using slicing, now use for loop and conditional execution to do it.

I have been trying unsuccessfully to use greeting.upper() to no avail!!

役に立ちましたか?

解決 2

That said, it's Friday and I'm feeling generous. Here's some code for (2):

>> greeting = 'Hello, world!'
>> new_greeting = ''
>> for ch in greeting:
..   if ch == 'o':
..     new_greeting += ch.upper()
..   else:
..     new_greeting += ch
..
>> print new_greeting
HellO, wOrld!

他のヒント

s = "Hello, world!"

print ' '.join([x.upper() if x == 'o' else x for x in s])

HellO, wOrld!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top