If a girl likes to see a dragon cat, then please cherish her, take care of her
Any language, the string is always the most basic and the most need to master a variable, think of the introduction of Hello World, the output is a string.
Official document: Https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str.
The string also becomes a sequence of characters,
I. Forms of expression 1.1 basic manifestations
Strings can be enclosed in single quotes, double quotes, and three quotation marks, with the following characteristics:
- Single and double quotes are similar, internally if you want to use it, you need to escape with a slash
- The three quotation marks are three single or double quotes that can be wrapped, meaning that all literal symbols are included
' Yiwangzhibujian ' " Yiwangzhibujian " " " Yiwangzhibujian " " """ Yiwangzhibujian """
The form above is the same, you can use the string class to create a string, the string class is Str
Name=str ('yiwangzhibujian')print(name)
This approach can be understood.
1.2 Other forms of expression
Cond
Second, common methods
For all the methods of the string, see official documentation: Https://docs.python.org/3/library/stdtypes.html#string-methods. Only a few of the most frequently used ones.
2.1 Common methods
Str.center (width[, Fillchar])
Mainly used to format the string, if width is less than the string length, return the original string, greater than the length of the string, the string is centered, the sides are filled with Fillchar, fillchar default to a space
Str.count (sub[, start[, end]])
: Returns the number of occurrences of the specified string
Str.encode (encoding= "Utf-8", errors= "strict")
Convert to byte array
Str.endswith (suffix[, start[, end]])
To interpret whether a string ends with a specified string
Str.find (sub[, start[, end]])
Returns the index of the specified string, no return-1
Str.join (iterable):
Use this string to connect an iterative object
Str.lower ():
Convert lowercase
Str.replace (old, new[, Count]):
String substitution, Count is the first few occurrences of the substitution, so the default is
Str.rfind (sub[, start[, end]]):
Returns the position of the last occurrence of a string
Str.split (Sep=none, Maxsplit=-1):
Split with specified characters, if specified count from left
Str.splitlines ([keepends]):
Split with line breaks, newline characters not included, line breaks include car
Str.startswith (prefix[, start[, end]]):
Determines whether a character starts with a specified character
Str.strip ([chars]):
Remove the specified character before and after the default is a space
Str.upper ():
Convert all characters to uppercase
2.2 Global methods
There are also some global methods that can function on strings:
Len ():
Get the length of a string
This method is still more commonly used.
2.3 Less common methods
Not commonly used methods also have a lot, a general understanding can be, after a need to see, as follows:
- Str.capitalize (): Returns the first letter of the string in uppercase form
- Str.casefold (): Returns the Casefolded form of the string (??? )
- Str.expandtabs (tabsize=8): Replacing a tab with a space
- Str.format (*args, **kwargs): Formatting strings
- Str.format_map (mapping): similar to Str.format
- Str.index (sub[, start[, end]): similar to find but not found error
- Str.isalnum (): Whether all are numbers or letters
- Str.isalpha (): Whether it's all letters
- Str.isdecimal (): Whether it's all numbers
- Str.isdigit (): Whether it's all numbers
- Str.isidentifier ():???
- Str.islower (): All lowercase
- Str.isnumeric (): Whether it's all numbers
- Str.isprintable (): whether all printable characters
- Str.isspace (): Whether it is a space
- Str.istitle (): Whether it is a title
- Str.isupper (): Whether it's all uppercase
- Str.ljust (width[, Fillchar]): Similar to Str.center (width[, Fillchar]), left padding
- Str.lstrip ([chars]): Delete the specified character on the left, the default is a space
- Static Str.maketrans (x[, y[, z]):???
- Str.partition (Sep): Splits the position of the first occurrence of the specified string
- Str.rindex (sub[, start[, end]): Similar to Str.rfind (sub[, start[, end]), but no error is found
- Str.rjust (width[, Fillchar]): And Str.ljust (width[, Fillchar]) a property, right padding
- Str.rpartition (Sep): and str.partition (Sep) A character, where the split string last appeared
- Str.rsplit (Sep=none, Maxsplit=-1): Split by the specified character, if the specified quantity starts from the far right
- Str.rstrip ([chars]): With Str.lstrip ([chars]) a property, delete right
- Str.swapcase (): Turn case to cross
- Str.title (): Capitalize each word's large initial letter
- Str.translate (table): Character Map using???
- Str.zfill (width):???
Third, string formatting
String formatting its practical place is not a lot, know how to use, now there is no demand, will be added later.
Iv. string Other knowledge 4.1 the nature of strings
In simple terms, a collection of ordered characters. So all methods that can be applied to a collection can be applied to a string:
Name='yiwangzhibujian'print('k' in Name)print('w' in name) for in name: print(i)======== console output ========Falsetrueyiwangzhibujian
View Code
The string is as immutable as Java.
4.2 String subscript
Strings can also support subscript access, starting with 0, there is a feature that can use negative numbers, 1 for the last one, and so on:
Name='yiwangzhibujian'print(name[0])print(name[-1]) ======== console output ========yn
Be careful when using to prevent the subscript from crossing.
4.3 String slices
Slices not only apply to strings, but also list, tuple and other collections, powerful, simple to use, to master the usage, the basic syntax is as follows:
STR[I:J]
- Contains the bottom boundary, not the upper boundary
- The bottom boundary defaults to 0
- The upper boundary defaults to the string length
In addition, The Shard also introduces a third value, stepping, so the default is not used when the step is 1, if the step is negative, indicating flashbacks for the Shard.
4.4 string concatenation
string concatenation using + is a common use, but the string and digital stitching will be error, it is necessary to convert the number in advance, str () to complete the conversion
"Python" Java Programmer learning Python (vii)-Text class details (string, str)