Consolidate the Python foundation (2)

Consolidate the Python foundation (2)
Python string

We know that strings are built-in objects of Python, used to store and represent text-based information. Through the study of "Introduction to Python", we have understood and mastered the following characteristics of strings:

1、 String is a constant

Just like a number, a string is also a constant, and its value is itself, so it is also a literal constant. In other words, you see what it is, what it is, and cannot be modified (in-situ).

E.g:

‘abc’

‘abc’

2、 String is a constant of sequence type

The reason why there is a "string" in the name of a string, we can understand that it generally contains more than one character, but most of the time, it may be composed of multiple or many characters. However, once these "multiple characters" are "stringed" together, it constitutes an independent string constant. In the computer's memory, it appears as a small piece of storage space, and the information it stores cannot be modified in situ.

Because it cannot be modified, it has a fixed character sequence, so it also becomes a sequence type data. Like other sequence data, it has the sequence of the sequence (index index), the index value (the character index[n] at the specified position), and the length of the sequence (len).

E.g:

‘abc’[0]

‘a’

len(‘abc’)

3

3、 A string can be assigned to a variable as a constant with a value

Since a string is a constant, it can be assigned to a variable, so that a variable has its value. When a string is assigned to a variable, in the computer's memory, a reference is actually added to the variable, and this reference points to the storage area of the string in the memory space.

E.g:

x = ‘abc’

x

‘abc’

All right,

After reviewing these common sense about strings,

Today we will focus on the operation of strings.

Python provides us with a large number of built-in functions (methods),

It is convenient for us when we need to process the string as necessary,

Can be easily done in one move.

This is also our practical application in Python programming,

Will encounter a lot of basic knowledge and skills that should be familiar and mastered.

1、 String size operation

(1) Case conversion

str.lower()# Returns the lowercase format of the str string.

str.upper()# Returns the uppercase format of the str string.

PS: Please note that this newly generated string does not add a reference to the original memory address, but regenerates a memory segment.

E.g:

' aBcDeF'.lower()

' abcdef'

' aBcDeF'.upper()

' ABCDEF'

(2) Hump conversion

**str.title()# **Returns a new string with the first letter of all words in the string uppercase and other letters lowercase

str.capitalize()# Return a new string with the first letter capitalized and other letters all lowercase

E.g:

print('ab XY'.title())

Ab Xy

print('abc DE'.capitalize())

Abc de

(3) Case reversal

str.swapcase()#All strings are case converted (uppercase-->lowercase, lowercase-->uppercase)

E.g:

print('abc XYZ'.swapcase())

ABC xyz

2、 String judgment operation

(1) Determine whether the string is a number, letter, letter or number

**str.isnumeric() **# Determine whether the string is a number (without decimal point)

**str.isalpha() **# Determine whether the string is a letter

**str.isalnum() **# Determine whether the string is a mixture of letters and numbers (excluding other characters)

E.g:

>>> print('34'.isnumeric())

True

print('abc'.isalpha())

True

print('a34'.isalnum())

True

(2) Determine whether the string or the first letter is uppercase or lowercase

**str.islower() **#Is the string uppercase?

**str.isupper() **#Is the string lowercase?

**str.istitle() **#Whether the first letter of each word in the string is capitalized and the other characters are lowercase?

PS: It is required that the string str must contain at least one string character (such as not a pure number), otherwise it returns False.

E.g:

print('a34'.islower())

True

print('AB'.isupper())

True

print('Aa'.isupper())

False

print('Aa Bc'.istitle())

True

print('Aa_Bc'.istitle())

True

print('Aa bc'.istitle())

False

print('Aa_bc'.istitle())

False

print('Aa BC'.istitle())

False

(3) Determine whether the string is empty, printable, and conforms to the identifier definition rules

str.isspace()#Whether the string is blank (space, tab, newline, etc.)

str.isprintable()#Whether the string is printable

str.isidentifier()#Whether the string meets the identifier definition rules

E.g:

(1) Determine whether it is blank. No character is considered blank.

print(' '.isspace())

True

print(' \t'.isspace())

True

print('\n'.isspace())

True

print(''.isspace())

False

print('abc'.isspace())

False

(2) Determine whether it is a printable character.

print('\n'.isprintable())

False

print('\t'.isprintable())

False

print('acd'.isprintable())

True

print(' '.isprintable())

True

print(''.isprintable())

True

(3) Determine whether the identifier definition rules are met: it can only start with a letter or underscore, and cannot contain any characters except numbers, letters and underscores.

print('abc'.isidentifier())

True

print('2abc'.isidentifier())

False

print('abc2'.isidentifier())

True

print('_abc2'.isidentifier())

True

print('_abc_2'.isidentifier())

True

print('_Abc_2'.isidentifier())

True

print('Abc_2'.isidentifier())

True

3、 Filling operation

(1) The string is centered (to both sides) and filled

str.center(width[, fillchar])

The string is centered, and the left and right sides are filled with fillchar so that the length of the entire string reaches the size specified by width.

fillchar defaults to spaces.

If the width is less than the length of the string, it cannot be filled and the string itself is returned directly, and a new string object will not be created.

For example: fill and center the string with underscores

print('abc'.center(5,'_'))

_ ab_

print('abc'.center(6,'_'))

__ abc_

(2) The string is left/right, and fills to the other side

str.ljust(width[, fchar])

Use fchar to fill the right side of the string to make the overall length width.

str.rjust(width[, fchar])

Use fchar to fill the left side of the string to make the overall length width.

PS: If you do not specify fchar, it will be filled with spaces by default. If width is less than or equal to the length of the string, it cannot be filled, and the original string is returned directly, and no new string object is created.

E.g:

print('xyz'.ljust(5,'_'))

xyz__

print('xyz'.rjust(5,'_'))

__ xyz

**(3) Fill with zero **

str.zfill(width)

E.g:

print('abc'.zfill(5))

00 abc

print('-abc'.zfill(5))

print('+abc'.zfill(5))

+0 abc

print('42'.zfill(5))

00042

4、 Substring search of string

(1) Return the number of occurrences of the substring in the string

str.count(sub[, start[, end]])

PS: The index number can be used to specify where to start the calculation (start) and where to end the calculation (end). The default index is calculated from 0, excluding the end boundary.

E.g:

print('aaxyzaabbcaa'.count('aa'))

3

(2) Determine what character the string starts or ends with

str.startswith(fix[, start[, end]])

Determine whether the string starts with fix

str.endswith(fix[, start[, end]])

Determine whether the string ends with fix

For example: when fix is a normal string

print('abcxyz'.startswith('abc'))

True

False, because the search range is limited to start from index 4

print('abcxyz'.endswith('xyz',4))

False

False, because the search range is'abcxy'

print('abcxyz'.endswith('xyz',0,5))

False

**When fix is a tuple, as long as any element in the tuple satisfies the condition of endswith, it returns True. **

'xyz' in tuple satisfies the condition

print('abcxyz'.endswith(('ab','xyz')))

True

Neither'ab' nor'xy' in tuple meets the conditions

print('abcxyz'.endswith(('ab','xy')))

False

(3) Whether the search string contains a certain substring

str.find(sub[, start[, end]])

str.rfind(sub[, start[, end]])

str.index(sub[, start[, end]])

str.rindex(sub[, start[, end]])

find() searches whether the string contains substring sub, if it contains, returns the index position of sub, otherwise returns "-1". You can specify the start and end search positions.

rfind() returns the position of the rightmost substring found, if only one or no substring is found, it is equivalent to find().

index() is the same as find(), the only difference is that when the substring cannot be found, a ValueError is thrown.

E.g:

' abcxyzXY'.find('xy')

3

' abcxyzXY'.find('xy',4)

-1

' xyzabcabc'.rfind('bc')

7

**You can use the in operator to determine whether the string contains the substring sub. It returns a Boolean value instead of an index position. **

' xy' in 'abxycd'

True

' xyz' in 'abxycd'

False

5、 Replace operation

(1) Replace the substring old in the string with the new string

str.replace(old, new[, count])

PS: If count is given, it means that only the first count old substrings are replaced. If the substring old cannot be found in str, it cannot be replaced, and the original string is returned directly without creating a new string object.

' abcxyzoxy'.replace('xy','XY')

‘abcXYzoXY’

' abcxyzoxy'.replace('xy','XY',1)

‘abcXYzoxy’

' abcxyzoxy'.replace('mn','XY',1)

‘abcxyzoxy’

(2) String mapping conversion (can be understood as a simple encryption method)

str.translate(table)

static str.maketrans(x[, y[, z]])

str.maketrans() generates a character-to-character mapping table, and then uses translate(table) to map each character in the string.

For example, now I want to do a simple encryption of "I love you", replace some of the characters with numbers, so that others don't know what the converted sentence means.

Step 1: Create a mapping table

maketrans() generates the mapping table

in_str='abcxyzlij'

out_str='123456789'

map_table=str.maketrans(in_str,out_str)

Step 2: Use the mapping table to implement mapping (encryption)

Use translate() to map

mylove = 'I love you'

result = mylove.translate(map_table)

print(result)

I 7ove 5ou

PS: Both x and y in maketrans(x[, y[, z]]) are strings, and the length must be equal. If maketrans(x[, y[, z]]) is given the third parameter z, each character in this parameter string will be mapped to None.

E.g:

in_str='abcxyz'

out_str='123456'

map_table=str.maketrans(in_str,out_str,'ar')

mylove='How are you'

result=mylove.translate(map_table)

print(result)

How e 5ou

6、 String split operation

(1) Divide into triples

str.partition(sep)

str.rpartition(sep)

Search for the substring sep in the string, and divide the string from sep, and finally return a tuple containing 3 elements: the left part of sep is the first element of the tuple, and sep itself is the second element of the tuple Element, the right side of sep is the third element of the tuple.

partition(sep) is divided from the first sep on the left, and rpartition(sep) is divided from the first sep on the right.

If sep is not found, two elements of the returned 3-element tuple are empty. partition() is the last two elements are empty, rpartition() is the first two elements are empty.

E.g:

print('abcxyzxyzopq'.partition('xy'))

(' abc', 'xy', 'zxyzopq')

print('abcxyzxyzopq'.rpartition('xy'))

(' abcxyz', 'xy', 'zopq')

(2) Split the string and generate a list

str.split(sep=None, maxsplit=-1)

str.rsplit(sep=None, maxsplit=-1)

str.splitlines([keepends=True])

split() splits the string according to sep, maxsplit is used to specify the number of splits, if maxsplit is not specified or the given value is "-1", it will search from left to right and split each time it encounters sep until the search is complete String.

If you do not specify sep or specify None, then change the segmentation algorithm: use spaces as separators, and compress consecutive spaces into one space.

**rsplit() and split() are the same, but search from the right to the left. **

**splitlines() is specifically used to split line breaks. Although it is a bit like split('\n') or split('\r\n'), they are different. **

'1,2,3'. split(',')

['1', '2', '3']

'1,2,3'. split(',',1)

['1', '2,3'] # Split only once

'1,2,,3'. split(',')

['1', '2', '', '3'] # Does not compress consecutive separators

'< hello><>'.split('<')

['', ' hello>', '>', 'world>']

'< hello><>'.split('<>')

['< hello>', '']

'1 2 3'. split()# When sep is not specified

['1', '2', '3']

'1 2 3'. split(maxsplit=1)

['1', '2 3']

' 1 2 3 '. split()

['1', '2', '3']

' 1 2 3 \ n'.split()

['1', '2', '3']

When explicitly specifying sep as a space, tab, or newline

' 1 2 3 \ n'.split(' ')

['', '1', '', '2', '', '3', '', '\ n']

' 1 2 3 \ n'.split('\t')

[' 1 2 3 \ n']

' 1 2\ n3 \n'.split('\n')

[' 1 2', '3 ', ''] # Pay attention to the last item in the list''

''. split('\n')

['']

' ab c\n\nde fg\rkl\r\n'.splitlines()

[' ab c', '', 'de fg', 'kl']

' ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)

[' ab c\n', '\n', 'de fg\r', 'kl\r\n']

**Compare split() with splitlines(): **

split()

''. split('\n')

[''] # Because there is no newline to split

' One line\n'.split('\n')

[' One line', '']

splitlines()

"". splitlines()

[] # Because there is no newline to split

' Two lines\n'.splitlines()

[' Two lines']

7、 join operation

str.join(iterable)

Use str strings to concatenate the strings in an iterable object (iterable).

Note that all the iterable must be of string type, otherwise an error will be reported.

If you are still a beginner in python and don't know what iterable is, but want to take a look at the specific syntax of join, then you can temporarily understand it as: string string, list list, tuple tuple, dictionary dict, set set.

E.g:

String

X1='python'

'_'. join(X1)

' p_y_t_h_o_n'

Tuple

X2=('1','2','3')

'_'. join(X2)

'1_2_3'

**set. **Note that the collection is out of order.

X3={'p','y','t','h','o','n'}

'_'. join(X3)

' n_o_p_h_y_t'

List

X4=['py','th','o','n']

'_'. join(X4)

' py_th_o_n'

dictionary

X5={'name':"malongshuai",'gender':'male','from':'China','age':18}

'_'. join(X5)

' name_gender_from_age'

The part of iterable that participates in iteration must be of string type, and cannot contain numbers or other types.

8、 Trimming operation

Remove the char on the left, right, left and right sides. If chars is not specified or None is specified, blanks (spaces, tabs, newlines) are removed by default.

str.strip([chars])

str.lstrip([chars])

str.rstrip([chars])

The only thing to note is that chars can be a sequence of multiple characters. When removing, all characters in this sequence will be removed.

**For example: remove single characters or blanks. **

' abcde '.strip()

' abcde'

print('www.example.com'.lstrip('cmowz.'))

example.com

Recommended Posts

Consolidate the Python foundation (2)
Consolidate the foundation of Python(7)
Consolidate the foundation of Python(6)
Consolidate the foundation of Python (3)
2.3 Python language foundation
python guess the word game
Ubuntu install the latest Python 3.
&quot;Consolidating the Foundation of Python&quot; (1)
Python realizes the guessing game
The difference between Python extensions
Python implements the brick-and-mortar game
The usage of wheel in python
How Python implements the mail function
How does python change the environment
Python simply implements the snake game
Python handles the 4 wheels of Chinese
Python3 implements the singleton design pattern
How to save the python program
Python simulation of the landlord deal
What is the use of Python
​What are the numbers in Python?
Python implements the steepest descent method
Talking about the modules in Python
The premise of Python string pooling
Secrets of the new features of Python 3.8
How Python implements the timer function
How to view the python module
Python implements the aircraft war project
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
End the method of running python
Install the latest Python 3.6 version on Ubuntu
Understanding the meaning of rb in python
Can Python implement the structure of the stack?
Why doesn&#39;t Python support the switch statement?
What are the required parameters of python
How does python improve the calculation speed
Logistic regression at the bottom of python
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
Python string to judge the password strength
Solve the conflict of multiple versions of python
What is the scope of python variables
Can I learn python without programming foundation
Python implements the sum of fractional sequences
Two days of learning the basics of Python
What is the id function of python
Python basic actual combat-guess the age game
Where is the pip path of python3
python implements the gradient method python the fastest descent method
How does Python handle the json module
What are the advantages of python language
The specific method of python instantiation object
Why doesn&#39;t Python support the switch statement?
Python novice learns to use the library
python3 realizes the function of mask drawing
How to learn the Python time module
What is the prospect of python development
Python realizes the greedy snake double battle
What is the function body of python