want help in string operation i want to do like this temp='faisal' OR temp=['faisal'] OR temp=["faisal"] now i want to change letter f to something else how can i do that??
string objects dont support item assignment so you need to do something like this: temp = "d" + temp[1:] for the list you could do something like this: temp[0] = "d" + temp[0][1:] PS: temp=['faisal'] and temp=["faisal"] are exactly the same thing so you dont need to tell them apart.
what if i want to replace all consonants from a word to "-" like faisal >>>-ai-a- like this and i have a list of names like this name=['faisal,'raza'','life','saver'] and i have to replace all consonants with "-"
what I would do in that situation is first create a list of all the consonants, in the form listConsonants = [b,c,...] then I'd create a variable for the new list rearrangedList = [] then I'd use a loop: for each word in the wordlist: #to get words rearrangedWord = '' # empty string so we've got a place to put the word as we're building it for each letter in word: #iterate over the letters in the word if the letter isn't in the consonants list: # if the letter is a vowel add the letter to the rearranged word if the letter is in the consonants list: # if the letter is a consonant append the character '-' in place of the letter add the rearrangedWord to the rearrangedList
thanks bro now lemme implement this :)
there are lots of was to implement it. this might work.. names = ["faisal", "brian", "mary"] newListOfNames = [] def replaceConsonants(name): consonants = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"] for consonant in consonants: newName = name.replace(consonant, "-") name = newName return newName for name in names: newname = replaceConsonants(name) newListOfNames.append(newname) print names print newListOfNames
thanks somnamniac i have got it like this: movies={'hollywood':['matrix','paycheck','ninja assasin'],'bollywood':['omkara','tis maar khaan','ddlj']} #print movies test=[] for i in movies.values(): for j in i: print j test.append(j) print test vowels=['a','e','i','o','u'] retest=[] for i in test: reword='' for j in i: ans=False for v in vowels: if j==v: ans=True if ans==False: reword='-'+reword[:] else: reword=j+reword[:] retest.append(reword) print retest
thanks axl456
Join our real-time social learning platform and learn together with your friends!