0
creating a string from the items of secondary if main string met the condition
Hi, I work on a code and i have difficulties on a part of it. There is main string: '(a bc defg.)' The secondary string: 'iamhere' I want to create a new string by comparing these two strings and get: '(i am here.)' condition: if item in main string is not a letter, then add item to new string. Else add item of secondary string into the new string i could not do it for loop using isalpha function. Anybody have an idea how can i achieve the desired string?
6 Réponses
+ 3
mst="abcde."
sst="hello"
newst=sst + "".join(i for i in mst if not i.isalpha())
print(newst)
+ 2
(i am here.) make sense
o='(a bc defg.)'
s='iamhere'
s =list(s)
for i in range(len(o)):
if not o[i].isalpha():
s.insert(i,o[i])
print(''.join(l for l in s))
+ 1
Hercule Poirot
you can’t get (hello.) by comparing this strings there is no “l” and “o” letters
0
@ simba
if item in main string is not a letter, then add item to new string. Else add item of secondary string into the new string
0
ubai
not exactly. actually there are parenthesis too. i edited my sample strings to make my question clearer.
0
ubai ty a lot it worked 😀