How the string capitalize ?? I know title() function but it create problem if string contain number as "1gjmk" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How the string capitalize ?? I know title() function but it create problem if string contain number as "1gjmk"

31st Dec 2019, 3:28 PM
AARTI SHELAR
AARTI SHELAR - avatar
33 Answers
+ 4
Alright I give up trying to explain what I mean 😂. Here is the code: S = "1utdhx 3g f g" new_S = "" for word in S.split(): if word[0].isdigit(): new_S += word else: new_S += word.title() You change .title() to .upper() if you want the whole word to be capitalized. (Haven't tested it, tell me if it doesn't work)
31st Dec 2019, 3:48 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Where is your ttempt ? What have you tried to solve this problem ?
31st Dec 2019, 3:32 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
AARTI SHELAR I know what you want, and that is not what I was asking about. I want to see what you have tried, so I can fix it. In case you haven't yet, try to split the sentence with .split() Then, loop throught letters of each word, and capitalize them, if they are not numbers.
31st Dec 2019, 3:36 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Oops sorry, all you have to do is add a space character at the end of the for loop.. new_S += " " This is exactly what I mean. If you couldn't figure out how to add a space in my code, then you need to do more problems, without the use of built-in methods.
31st Dec 2019, 3:58 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
AARTI SHELAR post the link here, so everyone can benefit from your post (if it isn't a duplicate already)
31st Dec 2019, 4:00 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
AARTI SHELAR my code works, except for one case. You should check how many spaces there are, because my code adds one space only after each word. Try to think of how to overcome spaces. You should be able to do it with your current python knowledge. You can share you attempt here if you still fail, but I want to see something that is close. I don't want to just give an answer, because it won't benefit you.
31st Dec 2019, 4:22 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
1. Loop throught the setence character by character (starting from second character) 2. Each time, you have two things to check; 1) Check that the character right before is a space 2) The character itself is a letter, and not a number or space 3. If these conditions are satisfied, capitalize the letter, else keep it as it is
31st Dec 2019, 4:59 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Guys, I just got to this thread and have read that challenge link. But I'm not getting why spaces is a problem, so what about this space problem?
31st Dec 2019, 5:55 PM
Ipang
+ 2
Ipang maybe you did it differently then :). At first, I split up the input and then joined it again using spaces. The problem is that in some cases there was more than just one space separating words, so my output was missing some spaces. It is an easy thing to resolve, so I wanted him to try it out first, considering I already showed him how to.
31st Dec 2019, 5:59 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
noor ul hudah upper capitalizes all the letters, not just the first one
1st Jan 2020, 4:11 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Your attempt?
31st Dec 2019, 3:30 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
31st Dec 2019, 4:02 PM
AARTI SHELAR
AARTI SHELAR - avatar
+ 1
31st Dec 2019, 4:11 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
AARTI SHELAR alright, good luck!
31st Dec 2019, 4:35 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
AARTI SHELAR That didn't help clarify unfortunately, maintain?
31st Dec 2019, 5:58 PM
Ipang
+ 1
So, finally problem solve regex rock🤗
2nd Jan 2020, 3:29 PM
AARTI SHELAR
AARTI SHELAR - avatar
0
What?
31st Dec 2019, 3:31 PM
AARTI SHELAR
AARTI SHELAR - avatar
0
S="1utdhx 3g f g" print(S.title()) # 1Utdhx 3G F G # I want 1utdhx 3g F G
31st Dec 2019, 3:34 PM
AARTI SHELAR
AARTI SHELAR - avatar
0
I already try capitalize() function but It doesn't maintain space after split(),while we use join it doesn't return space separate string I mean it doesn't maintain original string
31st Dec 2019, 3:40 PM
AARTI SHELAR
AARTI SHELAR - avatar
0
U are not getting my problem I want to maintain space as original string ?? How you determine how much space user give in string??
31st Dec 2019, 3:56 PM
AARTI SHELAR
AARTI SHELAR - avatar