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'
Similar, but slightly different, to another response using str.join() and string formatting:
>>> word = "hello" >>> print(f"_{'_'.join(word)}_") _h_e_l_l_o_ >>>
perhaps missing the augmented assignment operator +=
word = "hello" new_word = "_" for letter in word: new_word += letter + "_" print(new_word)
Look into Python’s assignment operators.
"".join([f"_{i}" for i in "hello"])+"_" '_h_e_l_l_o_'
thank you both!
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
but i am not getting _h_e_l_l_o_. this. what am i doing wrong ?
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.