Python basis (String function usage)

title()

Role: The first letter of a string of English words in all uppercase

eg:

>>> test1 = ' my  name is Alex'
>>> print(test1.title()) 

 My  Name Is Alex

strip()

Role: delete the beginning and end of the string all the special characters "\ n, \ r"

eg:

>>> test = "\n \n \n hello honey \n xixi \n nishi \n djaoifa \n"

>>> print(test.strip())

hello honey 
 xixi 
 nishi 
 djaoifa

lower()

Role: Converts a string in all lowercase English words

eg:

>>> test1 = 'my  Name iS Alex'
>>> print(test1.lower()) 

  my name is alex

upper() 

Role: to convert a string of all English words to uppercase

eg:

>>> test1 = ' My  Name iS Alex'
>>> print(test1.upper()) 

 MY NAME IS ALEX

center()

Action: the left and right in the middle of the string and the number of characters specified

eg:

>>> test1 = ' i  Name iS Alex'
>>> print(test1.center(40,"-"))

 ------------ i Name iS Alex------------

light()

Role: left-aligned, not enough can use other supplements

eg:

>>> test1 = ' i  Name iS Alex'
>>> test2 = 'dadaddada'
>>> print(test1.ljust(40,"-"))
>>> print(test1.ljust(40))
>>> print(test2.ljust(40,"*"))

 i  Name iS Alex------------------------
 i  Name iS Alex                        
dadaddada*******************************

rjust ()

Role: right-aligned, not enough can use other supplements

eg: 

>>> test1 = ' i  Name iS Alex'
>>> test2 = 'dadaddada'
>>> print(test1.rjust(40,"-"))
>>> print(test1.rjust(40))
>>> print(test2.rjust(40,"*"))

  ---------------------------- i  Name iS Alex
                                     i  Name iS Alex
*******************************dadaddada

count()

: The role of the number of characters appear in statistics

eg:

>>> test1 = 'this is test'
>>> print(test1.count("t"))
>>> print(test1.count("t",0,10))

3
2

encode()

Role: to solve the problem of decoding

eg:

Test1 = >>> 'the this Test IS'
>>> = test2 'This is a test'
>>> Print (test1.encode (encoding = 'UTF8', errors = 'sreict'))
>>> Print (test2. encode (encoding = 'utf8', errors = 'sreict'))

Is test b'this'
b '\ xe8 \ XBF \ x99 \ xe6 \ x98 \ XAF \ xe4 \ xb8 \ x80 \ xe4 \ xb8 \ XAA \ xe6 \ xb5 \ x8b \ xe8 \ XAF \ X95'

decode()

Role: resolve character encoding issues

eg:

>>> test1 = 'this is test'
>>> test2 = '这是一个测试'
>>> str1 = test1.encode(encoding='utf8',errors='sreict')
>>> str2 = test2.encode(encoding='utf8',errors='sreict')
>>> print(str1.decode(encoding='utf8',errors='sreict'))
>>> print(str2.decode(encoding='utf8',errors='sreict'))

this is test
this is a test

find()

Role: return to find the index of the string

Find the opposite direction "rfind ()"

eg:

>>> test1 = 'this is test'
>>> print(test1.find("i"))
>>> print(test1.find("i",4))
>>> print(test1.find("0"))

2
5
-1

format()

Action: Returns a formatted string

eg:

>>> test1 = '{first} this  is {number} test'
>>> print(test1.format(first="teststring",number="python"))

teststring this  is python test

endswith()

Role: to determine whether the string ends with the specified character

eg:

>>> test1 = 'this  is test'
>>> print(test1.endswith("python"))
>>> print(test1.endswith("test"))

False
True

startswith()

Role: to determine whether the string beginning at a specified character

 eg:

>>> test1 = 'this  is test'
>>> print(test1.startswith("python"))
>>> print(test1.startswith("this"))

False
True

isalnum ()

Action: determining whether a string of numbers and letters

eg:

>>> test1 = 'thisistest123123'
>>> test2 = 'this is test 123 #$#$#$'
>>> print(test1.isalnum())
>>> print(test2.isalnum())

True
False

Islf ()

Action: detecting whether a string consisting only of letters

eg:

>>> test1 = 'thisistest123123'
>>> test2 = 'thisistest'
>>> print(test1.isalpha())
>>> print(test2.isalpha())

False
True

isdigit()

Action: detecting whether a string consisting only of numbers

eg:

>>> test1 = 'thisistest123123'
>>> test2 = '123412312341'
>>> print(test1.isdigit())
>>> print(test2.isdigit())

False
True

islower()

The role: whether all lowercase letters

eg:

>>> test1 = 'thisistest'
>>> test2 = 'Tasdasd123'
>>> print(test1.islower())
>>> print(test2.islower())

True
False

isupper() 

The role: whether all uppercase letters

eg:

>>> test1 = 'thisistest'
>>> test2 = 'THISAHHA123'
>>> print(test1.isupper())
>>> print(test2.isupper())

False
True

ispace()

Role: detecting whether or not constituted only by spaces

eg:

>>> test1 = '        '
>>> test2 = 'THISAHHA123'
>>> print(test1.isspace())
>>> print(test2.isspace())

True
False

partition()

Action: dividing a specified character, returns a tuple

From right to left "rpartition ()"

eg:

Str = >>> "https://www.baidu.com/"
>>> Print (str.rpartition ( ": //"))
>>> Print (str.rpartition ( ",")) # string str sep absence of ",", two returns an empty string

('https', '://', 'www.baidu.com/')
('', '', 'https://www.baidu.com/')

split()

Action: to split the specified character string as a split return a list character

Instead "rsplit ()"

eg:

Str1 = >>> "https://www.baidu.com/"
>>> str2 = "I do Not Love Python"
>>> Print (str1.split ( "."))
>>> Print (str2. split ()) # is the default does not write blank

['https://www', 'baidu', 'com/']
['i', 'do', 'not', 'love', 'python']

splitlines()

Action: In \ n and \ r as the dividing delimiter strings, "True" parameter display symbols

eg: 

>>> str1 = """i
love
python
"""
>>> print(str1.splitlines())
>>> print(str1.splitlines(True))

['i', 'love', 'python']
['i\n', 'love\n', 'python\n']

join()

Action: The character string entered as the number of elements and between each element in the list, and form a new series of strings,

>>> list1 = ["abc","123","test","aadadad"]
>>> str1 = "*python*"
>>> print(str1.join(list1))

python abc * * 123 * * the test python python * * aadadad 

replace()

Action: The specified character string is replaced with a new character

Str1 = >>> "My My Happ adasdmy the Test"
>>> Print (str1.replace ( "My", "# # Python"))
>>> Print (str1.replace ( "My", "# # Python" , 1)) # You can specify the number of replacement

#python# #python# happ adasd#python# test
#python# my happ adasdmy test

Published 65 original articles · won praise 16 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41674452/article/details/104051285