Solved! Go to Solution.
"".join([f"_{i}" for i in "hello"])+"_"
'_h_e_l_l_o_'
Look into Python’s assignment operators.
perhaps missing the augmented assignment operator +=
word = "hello"
new_word = "_"
for letter in word:
new_word += letter + "_"
print(new_word)
Similar, but slightly different, to another response using str.join() and string formatting:
>>> word = "hello"
>>> print(f"_{'_'.join(word)}_")
_h_e_l_l_o_
>>>
To avoid leading and trailing join symbols, you can try this:
word = "hello"
joinword = ""
for (idx,letter) in enumerate(word):
joinword += letter
if idx < len(word)-1:
joinword += "_"
print(joinword)
This will print 'h_e_l_l_o'
"".join([f"_{i}" for i in "hello"])+"_"
'_h_e_l_l_o_'
For example, for a string hello, the result should be _h_e_l_l_o_.
word = "hello"
new_word = "_"
for letter in word:
new_word = letter + "_"
print(new_word)
but i am not getting _h_e_l_l_o_. this. what am i doing wrong ? how do i fix this ?
thanks
Look into Python’s assignment operators.
For example, for a string hello, the result should be _h_e_l_l_o_.
word = "hello"
new_word = "_"
for letter in word:
new_word = letter + "_"
print(new_word)
but i am not getting _h_e_l_l_o_. this. what am i doing wrong ?
thanks
perhaps missing the augmented assignment operator +=
word = "hello"
new_word = "_"
for letter in word:
new_word += letter + "_"
print(new_word)
Similar, but slightly different, to another response using str.join() and string formatting:
>>> word = "hello"
>>> print(f"_{'_'.join(word)}_")
_h_e_l_l_o_
>>>
thank you both!
To avoid leading and trailing join symbols, you can try this:
word = "hello"
joinword = ""
for (idx,letter) in enumerate(word):
joinword += letter
if idx < len(word)-1:
joinword += "_"
print(joinword)
This will print 'h_e_l_l_o'