I. string basic syntax

Keywords: Python

------------Resume content start------------

1.type() is to verify the type of a variable. int is a numeric variable, str is a character variable

name = "yao"
Year = 1993
print( name + str(Year))
>>yao1993

2.ord() is a built-in function that can return a certain character,

chr() is the corresponding character based on the integer value

print(ord("a"))
print(chr(97))
>>97
>>a

3. Many string methods can be viewed through dir()

print(dir(str))
>>

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

4. Case conversion

name.swapcase() #Lowercase to uppercase, uppercase to lowercase

name.capitalize() #title case

name.upper()      #All capitals

name.lower()      #All lowercase

name.title()        #All caps

5. Retrieve the position of a character in the string

name.find("n")  #Find " n"Location, starting at 0
name.index("n")#Retrieve the position of an element in a variable, starting at 0

6. Retrieve the number of times a character appears in the string

print (name.count("n"))

7. Check the length of the string

print(len(name))

8. power

pow(x,y)=x**y=x Of y Secondary power

9. absolute value

print(bas(-2))
>>2

10. remainder

print(5%3)
>>2
print(divmod(5,3))#Quoting and remainder
(1,2)

11. Maximum or minimum

print(max(1,2,3))#Maximum
>>3

12. Integers, floating-point numbers, and complex numbers

int(x) #take x Change to integer, discard decimal part
float(x)#take x Change to floating point number, increase decimal part
complex(x)#take x Change to complex, add imaginary part

13. Rounding

x=5.453
print(round(x,2))#round(x,d) d is to keep several decimal places

14. Remove spaces or line breaks

name="\nYao\n"
print(name.lstrip())#Remove line breaks or spaces on the left
print(name.rstrip())#Remove line breaks on the right
print(name.strip()) #Remove all line breaks

15. judgement

print('ag12'.isalnum())#Determine whether it is composed of numbers and letters
print('ag'.isalpha())#Determine whether it is only a letter
print('2'.isdigit())#Judge whether it is only an integer
print('1'.isdecimal())#Determine whether it is decimal
print('a-1R'.isidentifier())#Determine whether it is a qualified identifier or variable name,No special characters, only integers and letters
print('My Name'.istitle())#Determine whether all letters are capitalized

16. fill

print(name.center(50,'%')) #Take 50 as width, insufficient'%'Fill, name In the middle
print(name.ljust(50,'%')) #Fill in% if insufficient, fill in later
print(name. rjust(50,'%'))  #Fill in% if insufficient

17. replacement

p = str.maketrans('abc','123')#use p Manufacturing substitution rules,
print('adbce'.translate(p))     #Then use translate Replace

print('alexa'.replace('a','A',1))#1 Replace several

18. Splicing and connection

1.split  #Return list
    b='www.baidu.com'
    c=b.split('.')
    print(c)
    >>['www', 'baidu', 'com']
2.join
    c=['www', 'baidu', 'com']
    d='*'.join(c)
    print(d)
    >>www*baidu*com

19. List sort, temporary sort, reverse list

a = [7,2,4,3,1]
a.sort()#The default is from small to large( reverse = True)From big to small

print(a)

print(sorted(a))#Temporary sort
print(a)
>>[1, 2, 3, 4, 7]

  >>[7, 2, 4, 3, 1]

a.reverse()#Reverse list

20. Modify list elements

Name = ['xiao','da','max']
Name[0] = 'You'
print(Name)
>>['You', 'da', 'max']

21. Insert and add elements

#Insertion element
Name = ['xiao','da','max']
Name.insert(0,'Apple')
print(Name)
>>['Apple', 'xiao', 'da', 'max']
#Additive elements
Name.append('blue')
 

22. Delete element

Name = ['xiao','da','max']
del Name[0]#Delete by index
print(Name)

print(Name.pop())#You can delete the elements at the end of the list and enable you to use the deleted elements
#You can also add indexes pop(1)
print(Name)
>>max
>>['xiao', 'da']

Name.remove('da')#Don't know the location, you can use remove()

23./n line feed, / t tab, / rreturn, / b fallback

Posted by mr666 on Thu, 12 Dec 2019 06:13:53 -0800