473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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?
May 16 '21 #1
2 5063
Banfa
9,065 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.
May 18 '21 #2
SioSio
272 256MB
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 + " "
May 19 '21 #3

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

Similar topics

1
by: Nick Ashton | last post by:
Hi Whilst browsing the MSDN on day, I am sure I saw an article on 'How to encode your ASP code within the asp page', but I can not find it again. Can anyone help me please. If I remember the...
1
by: Marco Leist | last post by:
hello, i´d like to replace certain strings in a XML-Document with other ones. Thereby the structure (tags)of the source-xml should be preserved completly, the strings that should be replaced are...
6
by: zoltix | last post by:
Hi, I would like to access to execute specific commands but the privileges are not enough from an aspx Page. Because this service run as IU_IISSERVER, therefore aspx hasn’t access to these...
0
by: Murthy | last post by:
I am using Visual Studio 6. My requirement is to search-and-replace in a string, with "whole word" option. I guess I should have grep source code. Can I find free code to do this somewhere? Thanks...
9
by: Sandy | last post by:
Hello - I need either a cheap tool or code & DB that calculates, eg. within 50-mile radius of a zip code. Anyone have any suggestions? -- Sandy
6
by: google | last post by:
I have a few general questions. I am working on a new database to be used within my company. I would like to give a couple of people, particularly HR, the ability to add and delete Access users,...
1
by: Robert Johnson | last post by:
Hi all. I need to format my text box's and combo box's with Telephone (###) ###-#### and Postal Code #####-#### or other custom formating, how do I do this? I tried to go into Properties,...
2
by: ignore.server.problems | last post by:
Does anyone know a trick to replace a keystroke (say "F12") with a series of keystrokes (say "some lengthy text followed by F12") using Visual Studio 2005? -Jeroen
13
by: Hongyu | last post by:
Hi, I have a datetime char string returned from ctime_r, and it is in the format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars including the last terminate char '\0', and i would...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.