Summary of python basic data types

Posted by gevans on Sun, 30 Jun 2019 20:50:21 +0200

1. String (str)

1>#Function: For capitalization, does not change itself, and generates a new value

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: used for capitalization, does not change itself, will generate a new value
user_name = 'tianjianyu'
new_user_name = user_name.capitalize()
print(new_user_name)

2>#Function: Change all capitals to lowercase, casefold s can also be used in other languages, such as German

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: Change all capitals to lowercase, casefold can also be used in other languages, such as German
user_name = 'TianJianYu'
new_user_name =  user_name.casefold()
print(new_user_name)

3>#Function: for converting all capitals to lowercase

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: Used to lower case all capitals, similar to casefold, but not in other languages
user_name = 'TianJianYu'
new_user_name = user_name.lower()
print(new_user_name

4>#Function: for text centering

#-*- coding:utf-8 -*-
#Function: for text centering
#Parameter 1: Represents the total length
#Parameter 2: Characters representing padding in white space (only unique)
user_name = 'tianjianyu'
new_user_name = user_name.center(20)
print(new_user_name)
 
user_name = 'tianjianyu'
new_user_name = user_name.center(20,'*')
print(new_user_name)

5>#Function: Used to count the number of occurrences of incoming characters

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: Used to count the number of occurrences of incoming characters
#Parameter 1: Value to find
#Parameter 2: Start position (index)
#Parameter 3: End (Index)
user_name = 'tianjianyu'
new_user_name = user_name.count('n',0,4)
print(new_user_name)

6>#Function: Used to determine if the end of a character has been passed in: (If yes, return to True, otherwise: Fasle)

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: Used to determine if the end of a character has been passed in: (If yes, return to True and vice versa: Fasle)
#Parameter 1: Incoming characters
user_name = 'tianjianyu'
new_user_name = user_name.endswith('yu')
print(new_user_name)

7>#Function: Used to determine if an incoming character begins: (If yes, return to True, otherwise: Fasle)

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: Used to determine if the incoming character begins: (If yes, return to True and vice versa: Fasle)
#Parameter 1: Incoming characters
user_name = 'tianjianyu'
new_user_name = user_name.startswith('tian')
print(new_user_name)

8>#Function: used to find the tab t, along with the characters in front of it

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: used to find the tab \t, along with the characters in front of it
#Parameter 1: Total length with leading characters
user_name = 'tian\tjian\tyu'
new_user_name = user_name.expandtabs(20)
print(new_user_name)

9>#Function: Index position used to find subsequences, returns -1 if none exists

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: To find the index position of a subsequence, return -1 if it does not exist
#Parameter 1: Characters used for lookup
user_name = 'tianjianyu'
new_user_name = user_name.find('j')
print(new_user_name)

10>#Function: for string formatting

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Function: for string formatting
# 1. Use of placeholders:
user_info = 'user:%s,age:%s,gender:%s'
new_user_info = user_info%('tianjianyu',23,'male')
print(new_user_info)
 
#2. How to use format:
user_info = 'user:{0},age:{1},gender:{2}'
new_user_info = user_info.format('tianjianyu',23,'male')
print(new_user_info)
 
#How format_map is used:
user_info = 'I am:{name},Age:{age},Gender:{gender}'
new_user_info = user_info.format_map({'name':'tianjianyu','age':23,'gender':'male'})
print(new_user_info)

11>#Function: Determine whether it is a number, letter, or Chinese character (if yes, the return value is True, otherwise Fasle)

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: Determine whether it is a number, letter, or Chinese character (if yes, the return value is True, otherwise Fasle)
num_name = 'tianjianyu12 male'
new_num_name = num_name.isalnum()
print(new_num_name)

12>#Function: Determine if it is a number

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: Determine if it is a number
num = '123'
v1 = num.isdecimal()    #123(*)
v2 = num.isdigit()      #123,②
v3= num.isnumeric()     #123, 2, 2
print(v1,v2,v3)

13>#Function: Determine if you can act as a variable

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: Determine if you can act as a variable
n = 'name'
v = n.isidentifier()
print(v)

14>#Function: Determine if all characters are case or not

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: Determine if all characters are case-sensitive
user_name = 'TIANJIANYU'
new_lower_name = user_name.islower() #Determine if all are lowercase (return value is Fasle)
print(new_lower_name)
new_upper_name = user_name.isupper() #Determine if all are capitalized (return value is True)
print(new_upper_name)

15>#Function: capitalize all letters

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: capitalize all letters
name = 'tianjianyu'
new_name = name.upper()
print(new_name)

16>#Function: Determines whether characters that cannot be printed, such as escape characters, will return Fasle

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: Determines whether characters that cannot be printed, such as escape characters, will return Fasle
user_name = 'tian\njian\nyu'
v = user_name.isprintable()
print(v)

17>#Function: Determine if all spaces are, if yes, return to True, otherwise Fasle

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: Judges whether all spaces are spaces, returns True if yes, and vice versa Fasle
name =' '
v = name.isspace()
print(v)

18>#Function: for string stitching

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: for string stitching
name ='tianjianyu'
new_name = '_'.join(name) #No element in inner loop followed by''
print(new_name)

user_list = ['tian','jian','yu']
new_user_list = '*'.join(user_list)
print(new_user_list)

19>#Function: for left and right filling

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: for left and right filling
name ='tian'
v1 = name.rjust(20,'*') #padding-left
print(v1)
v2 = name.ljust(20,'*') #Right Fill
print(v2)

20>#Function: Correspondence and translation for customization

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Functions: Used for custom correspondence and Translation
m =  str.maketrans('tian','1234') #Custom Relationships
name = 'tianjianyu' 
v = name.translate(m)  #Bring custom relationships into strings
print(v)

21>#Function: Split, and keep the split elements

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: divide, and keep the split elements
user_name = 'tian_jianyu'
new_user_name = user_name.partition('_')
print(new_user_name)

22>#Function: for replacement

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: for replacement
#Parameter 1: Characters to be replaced
#Parameter 2: Why replace characters
#Parameter 3: Several characters need to be replaced
user_name = 'tian|jian|yu'
new_user_name = user_name.replace('|','',2)
print(new_user_name)

23>#Function: for removing blanks or for customizing

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: used to remove blanks, or can be customized
name = 'tianjianyu '
new_name = name.strip()
print(new_name)

24>#Function: for case replacement of characters

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: for case replacement of characters
name = 'tianJIANYU'
new_name =name.swapcase()
print(new_name)

25>#Function: for fill, fill 0

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Function: for fill, fill 0
name ='tian'
new_name = name.zfill()
print(new_name)

26>

#!/usr/bin/env python
#-*- coding:utf-8 -*-
name1 = 'tian'
name2 = 'jian'
name3 = 'yu'
name = name1+name2+name3 #Function to perform u add_u
print(name)

2. Integer (int)

Topics: Python