In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the basic operation of Python string". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the basic operation of Python string".
1. Data of int type: basic operation +-* / * a = 3
Print (a.bit_length ())
Print (a)
B = 4
Total = a + b
Print (total) where bit_length () is used to calculate the binary length
In the input () we used yesterday, the user input is in the form of a string, but if we need to use the int type in the basic operation, we need to use strong conversion at this time.
For example:
A = 4
Print (a.bit_length ())
B = "4"
B = int (b) # here the string is converted into bit int type
Total = a + b
Print (total) 2. String (str)
What is a string? You can understand the content enclosed by double quotation marks, single quotation marks, three single quotation marks, three double quotation marks.
Example: MingXing = 'Jay Chou'
1. Indexing and slicing of strings
The index is the number of characters, and the index starts at 0.
There are two forms: 0 12 and-3-2-1
S = "jay"
Print (s [0])
[] here represents the index, s [0] gets j, s [- 1] represents the number from back to front, and the first one gets y.
Slicing is a difficult part of the content, and beginners are easy to get dizzy.
For example:
S = "We are learning python in Gao Shenghan's python official account."
S1 = s [4:8]
Print (S1)
The result of the output is "Gaosheng cold".
From this illustration, we can see that [4:8] refers to the beginning of index 4 and the end of index 8.
And we can also see that index 8 is not included. So we can come to a conclusion
S [4:8] cuts from left to right by default, [start,end) start represents the starting index, end represents the ending index, and does not include end.
So what if we write: what result can print (s [8:4]) output? according to your understanding, explain why s [8:4] didn't cut anything?
Next, let's look at two examples:
S3 = s [4:]
S4 = s [: 4]
Print (S3)
Print (S4)
[4:] means to cut from 4 to the end
[: 4] means to cut from 0 to 4 but does not contain 4
There is also a situation with step size.
S = "We are learning python in Gao Shenghan's python official account."
S5 = s [4:8:2]
# cut from index 4 to index 8, but does not include 8, step size is 2
Print (S5)
# output "high cold"
If the step size is negative, it means from right to left
S = "We are learning python in Gao Shenghan's python official account."
S5 = s [- 1RHUMU6RIMUL2]
# cut from index-1 to index-6 with a step size of 2
Print (S5)
# output "nhy"
Summary:
Indexes and slices:
The string [number] gets the character at the xx index position
The string [mdisplacement n] starts at m and ends with n. Never get married.
The string [m:n:s] ends from m to n. Take 1 for every s
1. Upper () ignores case
2. Split () string cutting. Blank cutting is used by default.
3. Replace () string substitution
4. Strip () removes the white space between the left and right ends.
5. Startswith () determines whether to start with XX.
6.find () lookup, cannot find return-1
7. Isdigit () determines whether it is made up of numbers.
Thank you for reading, the above is the content of "the basic operation of Python string", after the study of this article, I believe you have a deeper understanding of the basic operation of Python string, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.