Python learning path-string 1
1 string
The string type is the most common type in Python. We can create a quotation mark ('and "have the same effect) by including characters. A string is a scalar and an unchangeable type. A string is composed of independent characters, which can be accessed through the slice operation sequence.
1.1 create and assign values to strings
>>> aStr = 'hello world'>>> another = "hello csdn">>> aStr'hello world'>>> another'hello csdn'>>> s = str(range(5))>>> s'[0, 1, 2, 3, 4]'>>>
1.2 access string value
Python does not have this type of character, but is represented by a string with a length of 1. You can use the slice operation to obtain characters or substrings.
>>> aStr'hello world'>>> aStr[2]'l'>>> aStr[2:]'llo world'
1.3 change the string
You can change the string by assigning a value to the variable.
>>> aStr'hello world'>>> aStr = aStr[:6] + ' python'>>> aStr'hello python'>>>
The string type is immutable. To change a string, you must create a new String object.
1.4 Delete characters and strings
The string is unchangeable, so you cannot delete a character in a string. All you can do is Delete the entire string. If you want to delete a character, you can combine the unneeded strings into a new string. For example, if you want to delete 'O', you can use the del statement to delete the string.
>>> aStr = 'hello man'>>> aStr = aStr[:4] + aStr[5:]>>> aStr'hell man'>>> del aStr
2 string and operator 2.1 Standard Type Operator
>>> str1 = 'abc'>>> str2 = 'def'>>> str3 = 'kmn'>>> str1 < str2True>>> str1 != str2True>>> str1 < str3 and str2 == 'def'True>>>
Strings are compared based on the ASCII value.
2.2 sequence operator slice forward index:
>>> str = 'abcdefg'>>> str[0:3]'abc'>>> str[2:5]'cde'>>>
Reverse index:
>>> str[-6:-1]'bcdef'>>> str[-8:-1]'abcdef'>>>
Default index: If the index is started or the description index is not specified, the first and last index values of the string are the default values. If the two are not specified, the entire string is returned.
>>> str[:4]'abcd'>>> str[4:]'efg'>>> str[:]'abcdefg'>>>
2.3 The in (not in) member operator is used to determine whether a character or a substring appears in another string. If yes, true is returned. Otherwise, False is returned.
>>> 'bc' in 'abcd'True>>> 'n' in 'abcd'False>>> 'dd' not in 'abcd'True>>>
The 2.4 connector (+) can connect two strings to form a new string.
>>> 'this is str1' + ' this is str2''this is str1 this is str2'>>> s = 'xxx' + ' ' + 'is a good' + ' ' + 'student'>>> s'xxx is a good student'
This kind of connection is called the runtime connection, and another kind of connection is the compile-time connection (not commonly used, depending on personal habits)
>>> text = 'hello' 'world!'>>> text'helloworld!'>>>