One, the string operator
The following table instance variable a value is the string "Hello" and the B variable value is "Python":
operator |
Description |
Example |
+ |
String connection |
>>>a + b 'hellopython' |
* |
Repeating output string |
>>>a * 2 'hellohello' |
[] |
Getting characters in a string by index |
>>>a[1] 'e' |
[:] |
intercept part of a string |
>>>a [1:4] ' ell |
in |
member operator-if the string contains the given character returned True |
>>> "h " in a True |
not in |
member Operators- Returns True if the string does not contain the given character |
>>> "m" not in a true |
R/r |
Raw string-Raw string: all strings are used directly as literal meanings, without escaping special or non-printable characters. The original string has almost exactly the same syntax as a normal string, except that the first quotation mark of the string is preceded by the letter "R" (which can be case). |
>>>print r \ n ' \n >>> print r'\ n ' \n |
Code:
1A ="Hello"2b ="Python"3 4 Print "A + b output results:", A +b5 Print "A * 2 output results:", A * 26 Print "a[1] Output results:", a[1] 7 Print "A[1:4] Output results:", A[1:4] 8 9 if("H" incha):Ten Print "H in variable a" One Else : A Print "H is not in variable a" - - if("M" not incha): the Print "M not in variable a" - Else : - Print "M in variable a" - + PrintR'\ n' - PrintR'\ n'
The results of the program execution are:
1 2 A + b output Result: Hellopython 3 A * 2 output result: Hellohello 4 a[1] Output: E
5 a[1:4
] Output result: Ell
6
H in variable a
7
M is not in variable a
8
\n
9\ n
Second, Python string formatting
Python supports the output of formatted strings. Although this may use a very complex expression, the most basic usage is to insert a value into a string that has the string format of%s.
Code:
Print " My name is%s and weight is%d kg! " % ('Zara'
Run results
My name is Zara and weight are kg!
Third, Python string built-in function
If there is a string mystr = "Hello World sqyy"
1. Find
Detects if STR is included in MyStr, or returns 1 if the index value is returned.
Mystr.find (str, start=0, End=len (mystr))
2. Index
Just like the Find () method, only if STR does not report an exception in MyStr.
3. Count
Returns the number of times that STR appears in the mystr between start and end
Mystr.count (str, start=0, End=len (mystr))
4. Replace
Replace the str1 in mystr with the STR2 if count specifies that the replacement is not more than count times.
Mystr.replace (str1, str2, Mystr.count (STR1))
5. Split
Cut, with Str as a delimiter slice mystr, if Maxsplit has a specified value, only maxsplit substrings are delimited
Mystr.split (str="", 2)
6, Capitalize
Capitalize the first character of a string
Mystr.capitalize ()
7, StartsWith
Checks whether the string starts with obj, or returns True, otherwise False
Mystr.startswith (obj)
8, EndsWith
Checks whether the string ends with obj, or returns False if True.
Mystr.endswith (obj)
9, lower
Convert all uppercase characters in mystr to lowercase
Mystr.lower ()
10, Upper
Convert lowercase letters in mystr to uppercase
Mystr.upper ()
11, Ljust
Returns the left alignment of an original string and fills the new string with the width of length with a space
12, Rjust
Returns the right alignment of the original string and fills the new string with the width of the length with a space
Mystr.rjust (width)
13. Center
Returns the center of the original string and fills the new string with a space of length width
Mystr.center (width)
14, Lstrip
Delete the left mystr space
Mystr.lstrip ()
15, Rstrip
Remove spaces at the end of a mystr string
Mystr.rstrip ()
16, RFind
Similar to the Find () function, it is just looking from the right.
Mystr.rfind (str, Start=0,end=len (MYSTR))
17, Rindex
Similar to index (), but starting from the right.
Mystr.rindex (str, Start=0,end=len (MYSTR))
18, partition
The mystr is divided into three parts, str, str and STR
Mystr.partition (str)
19, Rpartition
Similar to the partition () function, but starts from the right.
Mystr.rpartition (str)
20, Splitlines
Returns a list containing rows as elements, separated by rows
Mystr.splitlines ()
21, Isalnum
Returns True if all mystr characters are letters or numbers, otherwise False
Mystr.isalnum ()
22, Isalpha
Returns True if mystr all characters are letters, otherwise False
23, IsDigit
Returns True if the mystr contains only a number, otherwise False.
24, Isspace
Returns True if the mystr contains only spaces, otherwise False is returned.
Mystr.isspace ()
25, Isupper
Returns True if all characters in the mystr are uppercase, otherwise False
Mystr.isupper ()
26. Join
Insert Str after each character in the mystr to construct a new string
1 mystr.join (str)
Common operations for "code learning" python strings