"".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'
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
but i am not getting _h_e_l_l_o_. this. what am i doing wrong ? how do i fix this ?
thank you both!
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und vieles mehr. Neu hier? Erstelle ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.