The usage of the string type in Python. I mainly want to sort out some ideas in this part. Many online users have shared their usage of the string type in Python.
String Representation
1: 'me' single quotes
2: The combination of "me" Double quotation marks is very JavaScript. This is indeed a lot easier to write. At the underlying Python compiler, it will be enclosed in single quotes.
3: ''' three quotation marks ('). This is generally used to write multiple lines of text. _ Doc _ use this. What makes me more direct is thatCodeHTML code.
4: R "messs", add an R to the front of the string, abbreviated as raw, which is a native character (turn off the Escape Character in the string ). This is used when writing the R "C: \ WINDOWS \ System32" path. It can also be used when print is displayed. In python, another function has the same function as rept ().
5: U "string operation in Python Syntax" indicates that this is a Unicode character. It is known to be internationalized or address encoding problems, but it is not used in specific places currently. I have encountered the encoding problem, but I have not solved it in this way. I also hope that the visitor can give me a practical application scenario to better understand it.
Escape Character content
String type conversion
Basically, STR () can be used to convert most objects. The specific conversion form is actually determined by the _ STR () _ function of the class. So the function written by myself finally implements this method to facilitate the call of STR.
String is a sequence
String is a sequence and is immutable ). Since string is a sequence, it is natural that slice can be used. S [0: 3,
A few special notes
1: s [:] is actually a new copy of the character, it feels useless, because the = value of the string type itself is a copy process, this should also be a small copy
2: s [] This is a strange thing. In fact, 2 indicates the step size.
3: s [:-1] This can reverse a character. "ABC" is changed to "CBA", but this is actually a problem. If you are using Chinese, there is no way to reverse it. It turns out to be garbled.
Strings can be directly connected in the "+" way.
String. Empty in string is special.
The string. Empty constant in C # is used here. Python is actually a "" null string, but this is different from many meanings in C #.
For example, a string is name = "ABC ". Result of function call
Name = 'abc'
Print name. Count ('') # --> 4
Print ''in name # --> true
Print name. Index ('') # --> 0
Print name. Find ('') # --> 0
String can use for and in
Because it is a sequence, you can use for-in, but in can also be used separately.
If "ABC" in STR:
Print "yes"
Formatted string output
I generally use simple,
Print "% S % d" % ('2', 1)
If you are interested, you can refer to the following reference article.
String Method
There are a lot of websites. Here I will directly link to "common Python string operations"
String Template Method
All those who have used C # know that the C # syntax contains the string. Format ("ssss {0} SSS {1}", 1, 3) replacement method. In python, I tried to find a similar method. (* ^__ ^ *) There are also python. Copy the example directly:
>>> From string import Template
>>> S = template ('$ who likes $ whe ')
>>> S. substitute (WHO = 'Tim', what = 'ung pao ')
'Tim likes Kung pao'
Refer:
Http://www.telitchina.com/www/12/2007-07/35.html