converting a list to string and printing it out python -
i trying convert first letter of each word of string uppercase in python. keep getting generator object @ 0x10315b8>
no post before seems answer question.
def capitalize(str): newstr = str.split(' ') newlist = [] word in newstr: if word[0][0] == word[0][0].upper(): newlist.append(word[0][0].upper()) newlist.append(word[0][1:]) newlist.append(" ") convert_first = (str(w) w in newlist) print(convert_first) capitalize(input("enter string"))#calling function
your problem lies in how trying make string out of list of strings. opposite of "splitting" string list "joining" list string.
def capitalize(str): newstr = str.split(' ') newlist = [] word in newstr: newlist.append(word[0].upper() + word[1:]) convert_first = ' '.join(newlist) print(convert_first) capitalize(input("enter string"))#calling function
note: made attempt have code close possible in question.
also, why there if
statement in code? in place you're capitalizing words capitalized , discarding rest since never make newlist.
Comments
Post a Comment