468,468 Members | 2,694 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,468 developers. It's quick & easy.

How to replace a code not by capitalizing the other ones?

Expand|Select|Wrap|Line Numbers
  1. sentence = str ( input ( "Enter a sentence:" ) )
  2. sentence = sentence.split ( )
  3.  
  4. new = ""
  5. for word in sentence:
  6.     wordi = ord ( word[ 0 ] )
  7.     cap = word[ 0 ]
  8.     a = chr ( (ord ( cap ) - 32) )
  9.     word1 = word.replace ( word[ 0 ] ,a )
  10.  
  11.     if wordi <= 122 and wordi >= 97:
  12.         new = new + word1 + " "
  13.     else:
  14.         new = new + word + " "
print ( new )
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
  1. sentence = str ( input ( "Enter a sentence:" ) )
  2. sentence = sentence.split ( )
  3.  
  4. new = ""
  5. for word in sentence:
  6.     wordi = ord ( word[ 0 ] )
  7.     cap = word[ 0 ]
  8.     a = chr ( (ord ( cap ) - 32) )
  9.     word1 = word.replace ( word[ 0 ] ,"" )
  10.  
  11.     if wordi <= 122 and wordi >= 97:
  12.         new = new + a + word1 + " "
  13.     else:
  14.         new = new + word + " "
  15.  
  16. print ( new )
But, the code turned out to be that the letter that is being repeated in the word will be deleted when printed.

The input:

helloh

The output:

Hello

How will I be able to make the code work?
4 Weeks Ago #1
2 4617
Banfa
9,052 Expert Mod 8TB
I think (not a Python expert) your problem is at line 9.

Expand|Select|Wrap|Line Numbers
  1.     word1 = word.replace ( word[ 0 ], a )
The meaning of this line is for all letters in word replace h with H; which the program duly does.

a is the capitalisation of the first letter of word and you only ever want to change the first letter so I think you could replace this line with

Expand|Select|Wrap|Line Numbers
  1.     word[ 0 ] = a
Just like to repeat that I'm not a Python expert.
3 Weeks Ago #2
SioSio
246 128KB
You need to change the part that Banfa points out and the if statement.
Expand|Select|Wrap|Line Numbers
  1.     word1 = word.replace ( word[ 0 ] ,a )
  2.     if wordi <= 122 and wordi >= 97:
  3.         new = new + word1 + " "
  4.     else:
  5.         new = new + word + " "
3 Weeks Ago #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Nick Ashton | last post: by
reply views Thread by Murthy | last post: by
2 posts views Thread by ignore.server.problems | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.