Expand|Select|Wrap|Line Numbers
- sentence = str ( input ( "Enter a sentence:" ) )
- sentence = sentence.split ( )
- new = ""
- for word in sentence:
- wordi = ord ( word[ 0 ] )
- cap = word[ 0 ]
- a = chr ( (ord ( cap ) - 32) )
- word1 = word.replace ( word[ 0 ] ,a )
- if wordi <= 122 and wordi >= 97:
- new = new + word1 + " "
- else:
- new = new + word + " "
I have been writing about a code that would capitalize all the first letter in the sentence without using the capitalize or upper function. The code that I wrote did appear to be alright when the letter in the word is not the same as the letter that I want to capitalize.
The input:
Hello world
The output:
Hello World
However, if the letter in the word is also the same as the letter that I want to capitalize, the letter within the word will also become capitalized.
The input:
helloh worldw
The output:
HelloH WorldW
I tried to switch the "a" variable inside the replacement and add a to new as well in the variable new in the if-else statement.
Expand|Select|Wrap|Line Numbers
- sentence = str ( input ( "Enter a sentence:" ) )
- sentence = sentence.split ( )
- new = ""
- for word in sentence:
- wordi = ord ( word[ 0 ] )
- cap = word[ 0 ]
- a = chr ( (ord ( cap ) - 32) )
- word1 = word.replace ( word[ 0 ] ,"" )
- if wordi <= 122 and wordi >= 97:
- new = new + a + word1 + " "
- else:
- new = new + word + " "
- print ( new )
The input:
helloh
The output:
Hello
How will I be able to make the code work?