day 8 course summary

day 8 course summary

String related functions: len, str, eval

Str (data) converts the specified data into a string (put quotation marks directly outside the printed value of the data)
eval (string) evaluates the result of a string expression

 str1 = '[10, 20, 30]'
    result = eval(str1)
    print(result)
    result.append(100)
    print(result)

Relevant methods:

1. join

String join (sequence) concatenates the specified string of elements in the sequence into a string (the elements in the sequence must be strings)

list1 = ['name', 'age', 'gender']
result = '+'.join(list1)
print(result)
result = '♥'.join('abc')
print(result)

Example:

list1 = [100, 12.8, True, 'abc']
#use'#'Connect the elements in the list: 100#12.8#True#abc
str1 = [str(i) for i in list1]
result1 = '#'.join(str1)
print(result1)
#Use all digital data in the list'#'connect into one character
str2 = [str(i) for i in list1 if type(i) in (int, float)]
result2 = '#'.join(str2)
print(result2)
2.split

String 1 Split (string 2) use string 2 in character 1 as the cutting point to cut the string (from back to front)

After cutting, it is a list. The tangent point is at the beginning or end or several consecutive tangent points, and the result is an empty string (')
String 1 Split (string 2, N) takes the first N string 2 in string 1 as the cutting point to cut the string

str1 = '*123*abc*ABC*456*def*DEF*'
result1 = str1.split('*')
result2 = str1.split('*', 4)
print(result1)
print(result2)
3.replace

String 1 Replace (string 2, string 3) replaces all string 2 in string 1 with string 3
String 1 Replace (string 2, string 3, N) replaces the first N string 2 in string 1 with string 3
Example:

```python
str1 = 'how are you? i am fine, think you, and you?'
    result1 = 'me'.join(str1.rsplit('you', 2))
    print(result1)   
str1 = 'how are you? i am fine, think you, and you?'
    str2 = str1[::-1]
    print(str2)
    str3 = str2.replace('uoy', 'em')
    str1 = str3[::-1]
    print(str1)
```
4. Replace characters

Str.maketrans (string 1, string 2) creates a one-to-one correspondence between all characters in string 1 and all characters in string 2
character string. translate (character correspondence table) replaces according to the relationship of the character correspondence table
Example:

str3 = 'Today is Monday, yesterday was 7, tomorrow is Tuesday. My favorite is Friday night and my least favorite is Monday morning'
table = str.maketrans('1234567', 'One, two, three, four, five, six days')
result = str3.translate(table)
print(result) 
5. Delete the blanks at both ends of the string

String strip() removes whitespace on both sides of a string
String rtrip() removes the white space to the right of the string
String ltrip() removes the space to the left of the string

6. Number of Statistics

String 1 Count (string 2) counts the number of occurrences of string 2 in string 1

Format string

1. Format string:

String containing format placeholder% (data 1, data 2, data 3,...); The data in () must correspond to the placeholder in the string one by one

2. Format placeholder

% s - string placeholder, which can correspond to any type of data
B.% d - integer placeholder, which can correspond to any number
% f - floating point placeholder, which can correspond to any number. If the decimal places are not controlled, all% s placeholders can be used
​ %. Nf - floating point number placeholder, which can correspond to any number and keep the number to N decimal places

Example:

# xxx, gender: x, age: xx, monthly salary: XXXXX 00 yuan!
# Variable = '% s', gender:% s, age:% d, monthly salary:% f yuan!'% (name, gender, age, money)
message = '%s, Gender:%s, Age:%d, a monthly salary: %.2f element!' % (name, gender, age, money)
print(message)

x = '%s-%d-%.2f' % (2.342, 2.342, 2.342)
print(x)
3.f-string
1. Basic usage

Syntax: f '{expression}' - splice the value of the expression in {} into the string as the string content

message = f'{name}, Gender:{gender}, Age:{age}, a monthly salary:{money}element!'
print(message)

a = 100
result = f'a:{100 + 200 + a}, b:{"abc"[-1]}'
print(result)
2. Add parameters

Syntax: f '{provide data expression: parameter}'

a. parameters for controlling decimal places
f '{expression:. Nf}' - keep N decimal places

money = 223.8
result = f'amount of money:{money:.2f}element'
print(result)

b. amount value display plus comma
Comma only: f '{expression:}'
Add a comma and control the number of decimal places: f '{expression:. Nf}'

money = 1800000
result = f'amount of money:{money:,}element'
print(result)       # Amount: 1800000 yuan

result = f'¥{money:,.2f}'
print(result)       # ¥1,800,000.00

c. display percentage:

f '{expression:. N%}' n controls the decimal places of the percentage

x = 0.34
result = f'growth rate:{x:.1%}'
print(result)
			d.Control length
				f'{expression:character>N}'

f '{expression: character < n}'

num = 8
result = f'py2101{num:0>3}'
print(result)

Keywords: Python Back-end

Added by blueman on Sun, 27 Feb 2022 15:36:32 +0200