In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to capitalize all English words with Python". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to capitalize all English words with Python.
Capitalizing the first letter of an English word is a very common text operation. Using the capitalize method, you can capitalize the first letter of an English word. But how do you capitalize the first letters of all English words in a paragraph? The easiest way to think of is to split these English words into separate words, and then use the capitalize method to capitalize the first letters of these English words, and then connect these words. The implementation code is as follows:
S = 'The weather is really nice today, very suitable for an outing.' Arr = s.split () for i in range (0, len (arr)): arr [I] = ARR [I] .capitalize () S1 = ".join (arr) print (S1)
Run the code to see the effect:
As you can see from this code, three methods are used: split, capitalize, and join. Used to split strings, convert the first letters of English words to uppercase, and merge strings in the list with specific delimiters (spaces in this case).
But this code is so troublesome, is there a simpler way? Of course there is, clang clang! Here comes the code that just came out of the pot:
S = 'The weather is really nice today, very suitable for an outing.' Print ("" .join ([word.capitalize () for word in s.split ()])) # uses only one line of code
Isn't that cool? there's only one line of code here. In fact, this line of code is not fundamentally different from the previous implementation method, but uses the way in Python to generate a list through for in statements, simplifying multiple lines of code into one line of code. Python is amazing. In fact, if you want to be familiar with Python API and don't even have to write a single line of code, you can solve it in one way. This is the string.capwords method, which belongs to the string module, so you need to import the string module first. The code is as follows:
Import string s = 'The weather is really nice today, very suitable for an outing.' Print (string.capwords (s)) # capitalizes the first letters of all words in the string
The default delimiter of the capwords method is a space. If these English words are separated by other symbols, you need to use the second parameter of the capwords method. See the following example:
Import string s = 'The,weather,is,really,nice,today,very,suitable,for,an,outing.' Print (string.capwords (s,',')) # English words separated by commas
This code uses the capwords method to convert the first letter of all English words separated by commas (,) to uppercase letters. The running results are as follows:
The,Weather,Is,Really,Nice,Today,Very,Suitable,For,An,Outing. At this point, I believe you have a deeper understanding of "how to capitalize all English words with Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.