From entry to prison-strings and related functions

Getting started N day

One element occupies one character, and one escape character occupies one character

strx = 'sdaw'
print(len(strx))  # 长度为4

strx = '  sdadw'
print(len(strx))  # 7
strx = '\tdadw'
print(len(strx))  # 5

Type conversion
str
All data can be converted into a string, add quotation marks outside the data printed value

strx = 100
print(type(str(strx)))  # <class 'str'>
name = 'dwa'
age = 12
messge = name + '今年' + str(age) + '岁'
print(messge)

When one or more parts of a string change, we can use string addition
operation, format string and F string to achieve this function.
Format string
replaces characters with format placeholders in the string Change the part of the string, and then use data to
assign values to the format placeholders.
Syntax:
string% (data1, data2, data3,...) with format placeholders
Description:
% fixed writing
() fixed writing , It can be omitted when there is only one
data. The number of data data must be consistent with the number of placeholders in the previous string, and the type must
correspond to the placeholder one-to-one. The
format placeholder and the corresponding data type are fixed
% s ----- String (can be other types)
%d ----- Integer
%f ----- Floating point %.Nf Floating point is reserved for N decimal places (rounded)
%c ----- Character encoding value

name = 'dwaddaw'
age = 13
money = 125
messge = '%s今年%d岁,月薪%.2f元' % (name, age, money)
print(messge)

The format method contains a string of {}. In the format (data 1, data 2, ...)
string {} is equivalent to a placeholder in the format string

name = 'dwaddaw'
age = 13
money = 125

# 用法1
messge = '{}今年{}岁,月薪{}元'.format(name, age, money)
print(messge)

# 用法2 {下标}   下标指的是获取format中第几个数据,从0开始
name = 'd带娃娃'
age = 132
money = 1250
messge = '{2}今年{0}岁,月薪{1}元'.format(age, money, name)
print(messge)

# {key}
name = 'd带娃娃'
age = 132
money = 1250
messge = '{i}今年{j}岁,月薪{n}元'.format(i=age, j=money, n=name)
print(messge)
Format constraints: {: constraints}, {subscript: constraints}, {key: constraints}

'''
Constraints.
Nf retain N decimal places (rounded)
characters>Nd
characters<Nd
'''

print('约束1:{:.2f}'.format(3.1415926))  # 约束1:3.14

print('约束1:{1:.7f}'.format(3.1415926, 3.14159))  # 约束1:3.1415900

X>NN represents the total number of digits X represents the elements that need to be supplemented> represents the direction in the front <represents the direction in the back

print('约束2:{:X>5}'.format(10))  # 约束2:XXX10 10占两位,补三个X,总共5位
print('约束2:{: >5}'.format(10))  # 约束2:   10

{:,} Separate groups of three digits with commas (usually used for the representation of big data)

print('约束3:{:,}'.format(10000000))  # 约束3:10,000,000
% Convert decimals into percentage data and retain 6 decimal places
.N% converts decimals into percentage data and retains N decimal places
print('约束4:{:%}'.format(10000000))  # 约束4:1000000000.000000%
print('约束4:{:.2%}'.format(10000000))  # 约束4:1000000000.00%
# f-string   format方法的简写 f'字符串内容'
name = 'xx'
age = 15
money = 100000
str1 = f'{name * 2}今年{age * 2}岁,月薪{money / 2}元'  # xxxx今年30岁,月薪50000.0元
print(str1)

# f-strig 加约束

name = 100
age = 15
money = 100000
str1 = f'{name * 20:,}今年{age * 2: >3}岁,月薪{money / 2:.3f}元'  # 2,000今年 30岁,月薪50000.000元
print(str1)

‘’’

Information search address

https://www.runoob.com/python/att-string-format.html
rookie tutorial python format formatting function
https://www.runoob.com/python/python-strings.html
rookie tutorial python string Build function
'''
string 1.find(string 2) Get the position of string 2 in string 1 for the first time (returns a positive subscript value)
if it is not found, it will return -1

 字符串1.find(字符串2,开始下标,结束下标)
str1 = 'how are you ?Imfine, Thankyou!'
str2 = 'you'
index1 = str1.find(str2)  # 从头开始找
index2 = str1.find(str2, -7)  # 从下标为-7的字符找到最后的字符
index3 = str1.find(str2, 9, len(str1))  # 从下标为9的字符开始找
print(index1, index2, index3)

# 字符串.join(字符串序列)
# 将序列中所有的元素用指定字符串拼接在一起产生一个新的字符串
list1 = ['name', 'adw', '1345']
new_str = ''.join(list1)
print(new_str)

String 1.replace(string 2, string 3) Replace all string 2 in string 1 with string 3 to generate a new string
string 1.replace(string 2, string 3, N ) Replace the first N strings 2 in string 1 with string 3

str1 = '1561123114abcd'
new_str = str1.replace('abcd', 'ABCD')
print(new_str)  # 123456ABCD
new_str1 = str1.replace('1', '*', 2)
print(new_str1)  # *56*123114abcd

str1 = '123456'
str2 = '+'
new_str = str1.replace(str1[0], str2)  # 将下标为0的字符替换成+
new_str2 = new_str.replace(str1[-1], str2)  # 将下标为-1的字符替换成+
print(new_str2)  # +2345+

String.split (cutting point) One end and one cutting point is an empty string.
If multiple cutting points are next to each other, an empty string will be formed

str1 = '1123456 11123456 123456'
print(str1.split('1'))  # ['', '', '23456 ', '', '', '23456 ', '23456']
print(str1.split('5'))  # ['11234', '6 111234', '6 1234', '6']

Control the number of cuts

print(str1.split(' ', 1))  # ['1123456', '11123456 123456']
print(str1.split(' ', 2))  # ['1123456', '11123456', '123456']

String.zill(N)
Convert the string into a new string of specified length, the original string is on the right, and the left is filled with 0

num = 34
print(str(num).zfill(4))  # 0034
print(str(num).zfill(6))  # 000034

Guess you like

Origin blog.csdn.net/weixin_44628421/article/details/108886040