Reputation: 13
I just stated learning Python. My teacher asked me to take this string "quick brown fox, jumps over the lazy dog!"
, iterate all letter using i in range()
function, and whenever I find a whitespace in the string I need to make the next character uppercase. So that the final output would be Quick Brown Fox, Jumps Over The Lazy Dog!
The teacher said that I may need to use len(mastering)-1
for this homework.
Below is my code. I don’t know why I see the uppercase letter and then the repeated lower case of the same letter. I don't know how to skip them from the string during looping. Any suggestions are greatly appreciated!
I tried using different sequences in the for loop function but none worked.
mystring = 'quick brown fox, jumps over the lazy dog!'
newstring = mystring[0].upper()
for i in range (1,len(mystring)):
newstring = newstring + mystring [i]
if mystring [i] == ' ':
newstring = newstring + mystring[i+1].upper()
print (newstring)
I am getting double letters. Here is the output -
Quick Bbrown Ffox, Jjumps Oover Tthe Llazy Ddog!
Upvotes: 1
Views: 1845
Reputation: 25489
Iterating over indices using for i in range(len(container))
is frowned upon in python. Instead use for element in container
to get the elements directly. If you need the index, you can use enumerate
to get the element and index.
As for your question: one approach would be to create a variable that remembers if your previous character was a space. If it was, make the current letter uppercase. If not, append it to the new string as-is.
Another point to remember is that when you append to a string repeatedly in a loop, e.g.: str1 = str1 + new_character
python needs to copy all characters from the old string, plus your new character, to the new string every time this operation is performed. It is more efficient to append all your characters to a list, and use str.join
to join all elements of that list in one shot after your iterations are done.
mystring = 'quick brown fox, jumps over the lazy dog!'
result = []
uppercase_next = True # Since you want to uppercase the first character, we default this to True
for char in mystring:
if uppercase_next:
result.append(char.upper())
else:
result.append(char)
uppercase_next = (char == " ") # char == " " figures out if the character is a space,
# Then we assign the result to the variable uppercase_next
newstring = "".join(result)
print(newstring)
Upvotes: 1
Reputation: 7128
You are very close! To debug the program it's often good to take the simple input and go line by line, drawing what is happening on some sheet of paper.
Here when your i == 6
mystring[i]
points to space. Condition is fulfilled, nestring
becomes "Quick" + "b".upper()
, great! But now i==7
and you add newstring
(which is Quick B) mystring[i]
which is b.
To fix it you could e.g. check if previous character is a space:
for i in range (1,len(mystring)):
if mystring [i-1] == ' ':
newstring = newstring + mystring[i].upper()
else:
newstring = newstring + mystring[i]
And as a bonus, one liner for that.
" ".join(word.capitalize() for word in mystring.split(" "))
Upvotes: 0