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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what is the use of strings in Python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
1. Basic operation of string
As mentioned earlier, all sequence (including string) operations (slicing, indexing, multiplication, membership checking, length, minimum, and maximum) apply to strings. It is important to note, however, that strings are immutable, so all element assignments and slice copies are illegal. For example:
Second, the basic format of the string
1. The first method: this starts with an example, as follows:
The "%" above is the conversion specifier, which indicates where to insert the value, and s means to format the value as a string. If the specified value is not a string, it is converted to a string using str. Other descriptors will result in other forms of conversion.
2. The second method:
Parameters that contain an equal sign are called keyword parameters, and in string formatting, keyword parameters can be seen as a way to provide values to named replacement fields.
3. The third method:
Using the string method format, each replacement field is enclosed in curly braces, which may contain a name and information about how to convert and format the corresponding value.
Third, how can I play with the format of the string?
1. Replace the field name
Provide format with the unnamed parameters you want to format, and use unnamed fields in the format string. At this point, the fields and parameters are paired sequentially. You can also specify a name for the parameter, which will be used in the corresponding replacement field. In addition, you can use an index to specify which field you want to use the corresponding unnamed parameters, so that the unnamed parameters can be used out of order.
2. Width, precision, and thousands of delimiters
For width: set the width of the field as needed, but the number and string are aligned differently.
For precision: precision is also specified as an integer, but it needs to be preceded by a period representing the decimal point. The type f is explicitly specified above, but both width and precision can be specified.
For the quantile: use a comma directly to indicate the need to use the quantile.
IV. String method
Many string methods are "inherited" from the module string. There are too many string methods, so here are only some of the most useful.
1. The center method: centers the string by adding padding characters (spaces by default) on both sides.
Note: the above 28 is the specified width, when the specified width is less than the string width, then both sides of the string will not be filled.
2. Find method: look for substrings in the string. If found, the index of the first character of the substring is returned, otherwise-1 is returned.
3. Join method: its function is opposite to split, which is used to merge the elements of the sequence.
Note: the elements of the merged sequence must all be strings.
4. Lower method: returns the lowercase version of the string
5. Replace method: replaces the specified substring with another string, and returns the replaced result.
Note: this method is especially useful when dealing with small errors and small details in batches.
6. The split method, which works the opposite of join, is used to split strings into sequences.
Note: if no delimiter is specified, it will be split by default at one or more consecutive white space characters (spaces, tabs, newline characters, and so on).
7. The strip method: deletes the white space at the beginning and end of the string (but not the middle white space) and returns the deleted result.
8. The translate method: the method translate replaces a specific part of a string like replace, but it can only replace a single character. The advantage of this method is that it can replace multiple characters at the same time, so it is more efficient than replace.
From string import maketrans # must call the maketrans function.
Intab = "aeiou"
Outtab = "12345"
Trantab = maketrans (intab, outtab)
Str = "this is string example.wowning examples!"
Print (str.translate (trantab))
The output is as follows:
Th4s 3s srr3ng 2x1mp12....w4w!!! This is the end of this article on "what is the use of strings in Python?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.