python 3

1. All operations on strings

1. int.bit_length

Check the number of significant digits converted to binary.

>>> print(int.bit_length(5)) # 101
3

 

2. center

Centered!

>>> s = 'mid'
>>> print(s.center(9, '-'))
---mid---

3. upper,lower

All uppercase, use it when entering the verification code !

>>> s = 'mid'
>>> print(s.upper())
MID

The same is true for lower.

4.startswith,endswith

Determine if it starts/ends with xx.

>>> s = 'liuangshizhendeshuai'
>>> print(s.startswith('liuang'))
True
>>> print(s.startswith( ' zhend ' , 9 ,)) # If it is judged to the end of the sentence, the number after the comma can be omitted.
True

The same is true for endswith.

5. swapcase

case flip

>>> s = 'liuANGshizhendeshuai'
>>> print(s.swapcase())
LIUangSHIZHENDESHUAI

6. title

The first letter of each word is capitalized, and words are separated by spaces, numbers, and symbols .

>>> s = 'liu ang1zhen)de~~shuai'
>>> print(s.title())
Liu Ang1Zhen)De ~~Shuai

7.find , index

Find the location of the written character.

>>> s = ' liuangzhendeshuai ' 
>>> print(s.find( ' n ' ))
 4 
>>> print(s.find( ' nn ' ))
 - 1 
>>> print(s.find( ' n ' , 5 , )) # Judge to the end, so the number after the comma is omitted.
9

You can only judge from left to right, and stop moving forward immediately after finding it.

If the relevant character is not found, the output is -1

The index is the same, but if the relevant characters are not found, an error will be reported!

8. strip

Remove spaces, newlines, and TABs at the front and rear ends .

>>> s = '\t                liu ang   \n   '
>>> print(s.strip())
liu ang

Notice! Newline, the shortcut key of tab is \n not /n. . .

Used as a website user when entering a username.

You can also remove the specified character, and stop going backward/forward immediately after encountering the specified character.

>>> s = 'aaaaliu angaaaaaaaa'
>>> print(s.strip('a'))
liu ang

 

9. split

By default, strings are truncated from spaces to form a list.

>>> s = ' liu ang zhen de shuai ' 
>>> print(s.split())
[ ' liu ' , ' ang ' , ' zhen ' , ' de ' , ' shuai ' ]
 >>> s = ' ah liu ah ang ah zhen ah ah ah ah ah shuai ah ' 
>>> print(s.split( ' ah ' ))
[ '' , ' liu ' , ' ang ' , ' zhen ' , ' de ' , ' shuai ' , '' ]

Note that if the front/back starts/ends with this character, an empty set will be cut out.

You can specify how many times to cut from left to right

>>> print(s.split( ' Yeah ' , 1 ))
[ '' , ' liu ah ang ah zhen ah de ah shuai ah ' ]

The default is -1, the maximum number of cuts.

Therefore: the split command can be used to convert str to list.

10. join

Add the specified characters to each stackable object separately . Objects composed of multiple elements are called stackable objects.

>>> s = ['liu', 'ang', 'zhen', 'de', 'shuai']
>>> print('~'.join(s))
liu ~ang~zhen~de~shuai

Note: writing rules! Write what you want to put in the front, and who you want to operate in the back.

Therefore: through the join command, you can convert list to str.

11. replace

>>> s = 'liuangzhendeshuailiuang'
>>> print(s.replace('liuang', '刘昂'))
Liu AngzhendeshuaiLiu Ang

The number of replacements can be specified.

>>> s = 'liuangzhendeshuailiuangliuangliuang'
>>> print(s.replace('liuang', '刘昂', 2))
Liu AngzhendeshuaiLiu Angliuangliuang

12.len,count

number of characters.

>>> s = 'liuangzhendeshuailiuangliuangliuang'
>>> print(len(s))
35

Counts the number of occurrences of an element.

>>> print(s.count('i',0, 15))
1

13. format

formatted output. .

the first method:

>>> print( ' I want {}, and {}. ' .format( ' The Ragdoll ' , ' A pretty Ragdoll ' ))
I want a ragdoll, and a beautiful ragdoll.

Second usage:

>>> print( ' I want {1}, and {1}. ' .format( ' The Ragdoll ' , ' A pretty Ragdoll ' ))
I want a beautiful ragdoll cat, and a beautiful ragdoll cat.

The third usage:

>>> print( ' I want {cat}, and {a_beautiful_cat}. ' .format(cat = ' The Ragdoll ' , a_beautiful_cat = ' A Beautiful Ragdoll ' ))
I want a ragdoll, and a beautiful ragdoll.

14. isalnum,isdigitisalpha。

Determines whether the element consists of only letters or numbers/numbers/letters.

>>> print('1a ?'.isalnum())
False
>>> print('1'.isdigit())
True

15. capitalize

Capitalize the first letter and lowercase the rest!

two,

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325023119&siteId=291194637