Python3 string learning tutorial, python3 string tutorial
The string type is the most common type in python and is an unchangeable type. It supports single quotes, double quotes, and three quotes. The three quotes are a pair of continuous single quotes or double quotes, allowing a string to span multiple rows.
String connection: the + operator mentioned above can be used for string connection. You can also directly connect several strings and write them together. In addition, you can call the join () method to connect strings.
Only applicable to operators connected by strings: we have mentioned some operators shared by sequence types. In addition, strings also have their own operators, including the format control operator % and string template string. template, original string operator r/R, and Unicode string operator u/U.
The following lists some modules related to the string type.
String: Functions and tools related to string operations, such as the Template class.
Re: Regular Expression, powerful string mode matching module.
Struct: switch between the string and binary.
C/StringIO: String Buffer object. The operation method is similar to file object.
Base64: Base16, 32, and 64 data encoding/decoding.
Codecs: decoder registration and base class.
Crypt: Performs unilateral encryption.
Difflib: Find out the differences between sequences.
Hashlib: a variety of APIS for different security algorithm and information digest algorithms. python2.5 is abolished.
Hma: python Implementation of HMAC information authentication algorithm.
Md5: RSA digest authentication.
Rotor: provides multi-platform encryption and decryption services.
Sha: The security algorithm SHA of NIAT.
Stringprep: Provides Unicode strings for the IP protocol.
Textwrap: Text packaging and filling.
Unicodedata: Unicode database.
The default encoding method for Python 3 source code is UTF-8
In Python 3, all strings are Unicode-encoded character sequences.
UTF-8 is a method of encoding characters into byte sequences. A byte is a byte, not a character. Character is just an abstraction in a computer. A string is an abstract sequence. Here we only discuss strings, not bytes.
In Python, strings can be enclosed in single quotes, double quotes, or even three quotes.
However, if a string contains single quotes, you should use double quotation marks or use escape characters to enclose them with single quotes. The same is true for double quotation marks.
Character strings with three quotation marks can be used for line breaks!
>>> 'Let's go!' SyntaxError: invalid syntax >>> "Let's go!" "Let's go!" >>> 'Let\'s go!' "Let's go!" >>> '''''begin and next end''' 'begin\nand\nnext\nend'
The string cannot be modified. This is very important! You can think of it as a character tuples.
>>> s = "HelloWorld" >>> s[0] = 'h' Traceback (most recent call last): File "<pyshell#123>", line 1, in <module> s[0] = 'h' TypeError: 'str' object does not support item assignment
If you want to modify it, You can first convert it to a list, and then convert it to a string after modification.
>>> S 'helloworld' >>> L = list (s) >>> L ['h', 'E', 'l', 'l ', 'o', 'w', 'O', 'R', 'l ', 'D']> L [0] = 'H'> ''. join (L) # What is this? Don't worry. Let's talk about 'helloworld'
The string can be directly spliced, but if it is a string represented by two variables, you should use the plus sign.
>>> s = "Hello""World" >>> s 'HelloWorld' >>> s1 = "Hello" >>> s2 = "World" >>> s1s2 Traceback (most recent call last): File "<pyshell#138>", line 1, in <module> s1s2 NameError: name 's1s2' is not defined >>> s1+s2 'HelloWorld'
String operations and methods:
Len (s) returns the string length
X in s query whether x is in s
>>> s = "HelloWorld" >>> len(s) 10 >>> "ll" in s True
S. find (x) find the substring x in string s. If it is found, the leftmost index is returned. If it is not found,-1 is returned.
>>> s 'HelloWorld' >>> s.find("l") 2 >>> s.find('w') -1
S. splitlines () Splits multiple line strings into a list composed of multiple single line strings, and line breaks are absorbed
>>> s = '''''begin ...then.. ...next.. end...''' >>> s.splitlines() ['begin', '...then..', '...next..', 'end...']
S. split (x) Splits s into a string list using x as the separator. If x is not provided, the program automatically splits all spaces and line breaks as separators.
>>> S = "here # there" >>> s. split ('#') # As the separator ['here ', 'there'] >>> s = ''' begin. then .. and. next. end '''> s. split () # All line breaks and spaces are separated by default ['begin ','. then .. ', 'and ','. next. ', 'end']
S. lower () returns a string in the lower case of s.
S. upper () returns an uppercase string of s.
>>> s = 'HelloWorld' >>> s.lower() 'helloworld' >>> s.upper() 'HELLOWORLD'
The inverse method of s. join (seq) split. Sequence seq is connected by s. It must be a string sequence.
>>> L = ['1', '33', '42'] >>> '+ '. join (L) # join '1 + 33 + 42 'with +> L = list (s) # disassemble s> L ['h ', 'E', 'l', 'l', 'O', 'w', 'O', 'R', 'l ', 'D ']> ''. join (L) # bonded... 'Helloworld'
S. replace (x, y) replace all items matching x in s with y. If not found, s is returned directly.
>>> S 'helloworld' >>> s. replace ("World", "Cheng") 'hellocheng '> s. replace ("world", "Cheng") # know why... 'helloworld'
S. strip (x) If x is not provided, the string with spaces at the beginning and end is removed. If String x is provided, all characters at the beginning and end of s are removed and the string x is returned.
>>> S = "Hi, I'm Alice! ">>> S. strip ()" Hi, I'm Alice! ">>> S. strip ("! ") # This is two characters." Hi, I'm Alice "# an exclamation point is missing!
Note again: the above method does not change the original string, and the string cannot be changed!
Let's take a look at the following:
S. starstwith (x) test whether s starts with x
S. endswith (x) test whether s ends with x
S. isalnum () test if s is all letters and numbers with at least one character
S. isalpha () test if s is all letters with at least one character
S. isdigit () test if s is full of numbers with at least one character
S. isspace () test whether s is a white space character with at least one character
S. islower () test whether all letters in s are in lowercase
S. isupper () test whether the letters in s are uppercase letters
S. istitle () test whether s is capitalized
Let's focus on a powerful format method. See the following code.
>>> name = 'Jonh' >>> call = '13560300xxx' >>> "{0}'s telephone number is {1}".format(name,call) # (1) "Jonh's telephone number is 13560300xxx" >>> addr = "A103" >>> "{0}'s telephone number is {1} and his address is {2}".format(name,call,addr) #(2) "Jonh's telephone number is 13560300xxx and his address is A103"
(1) In the sentence, {0} in the string is replaced by the first parameter of format, and {1} is replaced by the second parameter. Are two parameters insufficient? In fact, you can give it any number of parameters, and then replace them with the same number of replacement fields. What is a replacement field? {0}, {1} is called the replacement field. We used three replacement fields in the (2) sentence. {0} corresponds to name, {1} corresponds to call, {2} corresponds to addr. More parameters are similar to the previous practice.
So, just like this? Of course not! Let's continue reading
>>> L = [2,3,5,7] >>> print("x is {0[0]}, y is {0[2]}".format(L)) x is 2, y is 5
{0 [0]} indicates L [0], {0 [2]} indicates L [2]. They are called compound field names. You can:
(1) Use a list as a parameter and use subscript indexes to access its elements (similar to the previous example)
(2) Use a dictionary as a parameter and access its value through a key
>>> d {'b': 2, 'a': 1} >>> print("x is {0[a]}, y is {0[b]}".format(d)) x is 1, y is 2 >>> d = {2:3.5,7:4.5} >>> print("x is {0[2]}, y is {0[7]}".format(d)) x is 3.5, y is 4.5
D is the dictionary, a and B are the keys, and {0 [a]} corresponds to the value 2 (Note: it is a, B, not 'a', 'B ')
(3) Use a module as a parameter and use its name to access its variables and functions
>>> print("{0.random}".format(random)) <built-in method random of Random object at 0x022D61F8>
(4) use the instance of the class as the parameter and access its method and attributes by name.
>>> class A: pass >>> x = A() >>> print("The class is {0.__class__}".format(x)) The class is <class '__main__.A'>
(5) any combination of the above methods
Besides integers, you can also use the parameter name to replace a field.
>>> print("{name}'s telephone number is {call}".format(name = "Jonh",call = 69993)) Jonh's telephone number is 69993
In the replacement domain, you can also use format specifiers. Colon: indicates the start of the format description.
>>> Pi = 3.141592653 >>> print ("The pi is {0: 10. 3f }". format (pi) #0: 10. 3f indicates that The output width is 10 and three decimal places are retained. The floating point number The pi is 3.142.