tags: # 0002 exam (Python) python Experience sharing String
Learn the basic usage and simple format of the string, let's take a look at the operations of common string types.
1, the operator of the string
Positive strings, Python provides 3 basic operators, as shown in the following table:
| Operator |
describe |
| x+y |
Connect two strings |
| x * n or n * x |
Copy the x replication N times |
| x in s |
If x is a sub-string of S, returns true, otherwise returns false |
Demonstration is as follows:
>>> x = 'one'
>>> Y = 'string'
>>> x+y
'A string'
>>> x*3
'One one one'
>>> s = 'a string'
>>> x in s
False
>>> y in s
True
2, string handler
The use of functions is convenient and fast, and Python has a lot of string handlers that use the string handle, which is commonly used:
| function |
describe |
| len(s) |
Returns the length of the string S, or the number of elements of other combination elements can also be returned. |
| str(s) |
Returns the string form corresponding to any type of S |
| chr(s) |
Returns the single character represented by Unicode Code S |
| ord(s) |
Returns Unicode encoding corresponding to single-character S |
| hex(s) |
Returns the string of the hex form of the integer S |
| oct(s) |
Returns the eight-way form of the integer S |
Demonstration is as follows:
>>> s = 'atrie string'
>>> LEN (S) # , punctuation is a character
5
>>> a =23
>>> str(a)
'23'
>>> chr(1010)
'ϲ'
>>> ord('ϲ')
1010
>>> hex(123)
'0x7b'
>>> oct(123)
'0o173'
4, string processing method
Method is a proprietary noun in programming, belonging to object-oriented programming. The computer secondary level test does not involve the object-oriented content in Python, so we don't have to study, it will be useful.
The method is also a function, just the way the call mode is different. The function is used in the <function name> (parameter), the method is <A>. <Function name> (parameter) to call, and previously passed the object <A> as input.
The following is a common string processing method:
| method |
describe |
| str.lower() |
Returns a copy of the string STR, all character lowercase |
| str.upper() |
Returns a copy of the string STR, all character capitalization |
| str.split(sep=None) |
Returns a list, splits according to the parameter SEP, missing, default is a space |
| str.count(sub) |
The number of times the sub-string SUB appears in the STR |
| str.replace(old,new) |
Returns a copy of the string STR, all sub-string OLDs are replaced by string New |
| str.center(width,fillchar) |
Function in string drama, FillChar parameter optional |
| str.strip(chars) |
|
| str.join(iter) |
Add a STR string after each element of the Iter variable |
Demonstration is as follows:
>>> Str = 'Python string'
>>> str.lower()
'python string'
>>> 'Python string'.upper ()
'Python string'
>>> str.split('t')
['Py', 'Hon string']
>>> str.count('Python')
1
>>> str.replace('on','eg')
'Pytheg string'
>>> Str.center (30, '+') # Set the width of 30, blank with '+' filled
'++++++++++ Python string ++++++++'
# String width is larger than Width, so the string width is displayed, and FillChar is missing the default space.
>>> str.center(2)
'Python string'
>>> '+'. Join ("python") # is very understandable, see the effect
'P+y+t+h+o+n'
>> Str = '++ == Python's normal characters == ++'
>>> str.strip(' ')
'++ == Python's normal characters == ++'
1, synchronous assignment Synchronous assignment refers to all expressions on the right side of the same operation equal sign, and assigns a variable corresponding to the left side of the correspondin...
Programming [question one] [Question 2] [Question 3]...
Problem Description: Fill in the blanket 1 Tip: The problems encountered herein are described here: For example, the data is lost during the data transmission, and some data will be lost occasionally....
Description Enter a line of characters, statistics, respectively, in English letters, spaces, numbers, and other characters. Input A line of characters Output Statistics Sample Input aklsjflj123 sadf9...
Python offers three digital types: integer, floating point, plural, corresponding to integers, real, plural 1, integer type By default, the integer uses decimal, other progress needs to be represented...
Summary of public basic knowledge of computer secondary python exam Chapter One Computer System The process of computer work is the repetition of the three basic actions of fetching instructions, anal...
important: Code is displayed in a picture form,Red box for the exam code in the examIt is also the code we need to add, and the rest of the code exam will be given, we need to fill in the remaining ar...
important: Code is displayed in a picture form,Red box for the exam code in the examIt is also the code we need to add, and the rest of the code exam will be given, we need to fill in the remaining ar...