USING PYTHON: Write a function called word_separator() that takes a single strin
ID: 674633 • Letter: U
Question
USING PYTHON: Write a function called word_separator() that takes a single string containing a sentence with all of the words are run together, and the first character of each word is uppercase. The function should return a single string that contains the converted sentence in which all the words are separated by a blank space and only the first word starts with an uppercase letter. (THIS IS FOR EXAMPLE < THE ANSWER SHOULD BE GENERAL AND SHOULD NOT INCLUDE THIS STATEMENT IN ANSWER) For example, the phrase “FourScoreAndSevenYearsAgo” would be converted to “Four score and seven years ago”, and “PassTheTNT’ would be converted to “Pass the t n t’. Your function may assume the original sentence has only upper and lower case letters – no punctuation, no white space, and no digits. No parameter testing is required. Note that an empty string is valid input, as is a string containing a single uppercase letter.
Explanation / Answer
def word_separator(string):
words = []
word = ''
for i in range(0,len(string)):
temp_char = string[i]
ascii = ord(temp_char)
if ascii>=65 and ascii<=90:
if len(word)>0:
words.append(word)
word = ''
word = word+temp_char
else:
word = word+temp_char
words.append(word)
print " ".join(words)
word_separator('FourScoreAndSevenYearsAgo')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.