In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Python string use method example analysis", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Python string use method example analysis"!
I. Character string
From the previous article: Python Standard Data Types, we already know: String is the most commonly used data type in Python, it consists of a series of characters, with single quotes 'or double quotes ">
Creating strings is simple, as follows:
var1 = "Hello World! "var2 = "Hello Python! "II. Indexing and slicing
In Python, to access a character or substring in a string, you need to use an index or slice, and the index value starts at 0.
(1)index
When we need to access a character in a string, we can use an index. We can use [ ] to get the value of a character in a string. There are two ways:
Index from front to back: Index values start with 0 and end with string length.
Backward index: index values start with-1.
For example, there are two ways to output the last character of the string "Python":
str = "Python"#Method 1: Index print from front to back (str[5])#Method 1: Index print from back to front (str[-1])
Output:
(2)slice
Slice is used when we need to access a substring within a string. We can use [ ] to get the value of a fragment (substring) in a string. There are two ways:
[start: end], take the substring fragment with index value from start to end, default step size is 1.
[start: end: step], take the substring fragment with index value from start to end, step size is step.
Two ways to get strings are as follows:
str = "123456"# [start:end]print(str[0 : 6])# [start:end:step]print(str[0 : 6 : 2])
Program output:
It can be seen that obtaining the substring by [start: end: step] will form a new substring with step interval values.
In addition, we need to master some default syntaxes:
For example, when we need to take the first 5 characters of a string, we can omit the first index str directly, as follows:
str = "123456"print(str[:5])
Similarly, when we take only the last five characters, we can write as follows:
str = "123456"print(str[-5:])
When we want to take the whole character, we can directly omit the first index and the last index, as follows:
str = "123456"print(str[:])
Strings cannot be updated by element assignment or slice assignment, such as the following:
website = "http://www.python.org"website[-3:] = "com"
Trying to update the original string website to "http://www.python.com" by slicing assignment is not allowed. The error is as follows:
Is it impossible to achieve the above functions? In fact, we can change the way we think, intercepting a part of the string and splicing it with other fields can be achieved, as follows:
website = "http://www.python.org"website = website[:-3] + "com"print(website)
Updated output:
IV. String operators
In the previous tutorial, we learned about the + and * operators. Let's continue to learn what other common operators are used in strings.
Operator description + string concatenation * string copy [] obtains a part of the string by indexing the character [:] in the string, following the principle of left closed right open, for example str[0:6] does not contain the 6th character. in Returns True if the string contains the given character, otherwise returns Falsenot in Returns True if the string does not contain the given character, otherwise returns False
Here is an example of how to use the above operators:
str1 = "Hello "str2 = "Python "print("str1 + str2 =", str1 + str2)print("str1 * 2 =", str1 * 2)print("str1[0] =", str1[0])print("str1[:3] =", str1[:3])if 'P' in str2:print("P is in string str2")else:print("P is not in string str 2")if 'P' not in str1:print("P is not in string str1")else:print("P is in string str1")
The output of the above example is:
V. String formatting
Python supports formatting string output, and string formatting in Python uses the same syntax as the sprintf function in C. Here is a simple example:
print("It is now %s year %s month" %('2021 ', ' 1'))
Result output:
In Python, there are also many string formatting symbols:
In addition, some formatting operator helper instructions are sometimes used:
Since Python 2.6, a new string formatting function str.format() has been added, which we will discuss later.
6. Common string built-in functions
There are so many built-in functions for strings, here are just a few of the most useful. For a complete list of string functions, see docs.python.org/3/library/string.html
6.1 Find substring find
The find function finds substrings in a string. If found, returns the index of the first character of the substring, otherwise returns-1. Examples are as follows:
6.2 lower and upper case
The function lower returns the lowercase version of the string, and the function upper returns the lowercase version of the string. Examples are as follows:
6.3 replace
The replace function replaces a specified substring with another string and returns the result. Examples are as follows:
6.4 merge join
The join function combines elements of a string sequence. Examples are as follows:
Note: \\is used here because the preceding\is an escape character that is not displayed.
6.5 split
The split function is a very important string method, which works the opposite of join and is used to split a string into sequences. Examples are as follows:
6.6 Delete blank strip
The strip function removes whitespace from the beginning and end of a string (but not the middle) and returns the deleted result.
6.7 center
The center function centers a string by adding padding characters (spaces by default) on both sides. Examples are as follows:
6.8 convert translate
The function translate replaces a specific part of a string like replace, but it can only do single-character substitutions. The advantage of this function is that it can replace multiple characters at the same time, so it is more efficient than replace. For example, in the following example, replace both characters "P" and "H" at the same time
At this point, I believe that everyone has a deeper understanding of "Python string use method example analysis", may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to 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.