+ 1

[Solved] it is possible to capitalize all sentences first letter of string?

srt1 = "hey i am python developer. this is new job. for me it nice to meet you" import re a1 = re.sub(r"[.]",",",srt1) print(a1) a1 = re.split(r"^,",srt1) print(a1) # i'm trying to split sentence through <'.'> but split is not working ... # if split will work , we can loop through each sentence[element] and apply capitalize() function # and join all sentence with <'.'> desired output :- "Hey i am python developer. This is new job. For me it nice to meet you"

16th Feb 2021, 6:35 AM
Hemant Kosrekar
Hemant Kosrekar - avatar
6 Réponses
+ 4
Yes, you can split first each sentences then use list comprehension to capitalize each sentence using str.capitalize() method. str1 = "hey i am python developer. this is a new job. for me it nice to meet you" print('. '.join([x.capitalize() for x in str1.split('. ')])) https://code.sololearn.com/ca13A20a18A7/?ref=app
16th Feb 2021, 6:48 AM
noteve
noteve - avatar
+ 4
Cyan Ya beat me to it, was about to post the same code. Glad I refreshed first! lol
16th Feb 2021, 6:54 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Me too.... Lol
16th Feb 2021, 6:55 AM
Tomas Konecny
+ 2
Thanks Cyan , Question solved .. but can you split through re.split() function . why re.split() not working that's the main question..??
16th Feb 2021, 6:55 AM
Hemant Kosrekar
Hemant Kosrekar - avatar
+ 1
Hemant Kosrekar Yeah, we can split it using the re module. x = re.split(r"\. ", str1) # Notice that we need to escape the dot (.) by backslash (\) if we want to match a literal dot. Though for capitalization, we still need to use str.capitalize() method, as using re.split and split will output the same result, so they're just basically the same (except that re.split can use more advance regex pattern).
16th Feb 2021, 7:00 AM
noteve
noteve - avatar
+ 1
thanks again ,Cyan to teach me regex concepts.
16th Feb 2021, 7:10 AM
Hemant Kosrekar
Hemant Kosrekar - avatar