Python includes the following common data types: numbers, Boolean values, strings, lists, tuples, dictionaries, collections, Collention series
First, the number
Keyword: int
Common ways to manipulate numbers:
A = "123" Print (int (a)) #转换成数字b = 5print (B.bit_length ()) #返回表示数字时占用的最少位数
Two, Boolean value
Keyword: bool
Represents true or FALSE, true or false
The indication of false: 0, "" (empty string), [] (Empty list), () (empty tuple), {} (empty dictionary)
A representation of true: non-zero number, non-null other data type Object
Three, string
Keyword: str
String Common functions:
- Remove whitespace
- Segmentation
- Length
- Index
- Slice
Common methods of string manipulation:
1, replace the operation:
test18 = "Alexalexalex" Print (Test18.replace ("Ex", "BBB")) #把字符串中指定的字串替换, the default is to replace all the print ("Ex", " BBB ", 2)) #把字符串中指定的字串替换, the third parameter specifies to replace the first few of these
2. Find operations
str1 = " Hello World" Print (Str1.find ("O")) #在字符串中查找指定的字串, returns its subscript, not found return -1print (Str1.index ("O")) # Finds the specified string in the strings, returns its subscript, and finds no return error
3. String formatting
Test1 = "I am {name}, age {A}" print (Test1.format (name= "Baihl", a=10)) #格式化输出test2 = "I am {0}, age {1}" print (test2.for Mat ("Baihl")) #格式化输出print (Test1.format_map ({' name ': "Baihl", ' a ':)) #使用字典方式格式化
4. Split operation
Test10 = "Heart Dream no Trace" str1 = "_" Print (Str1.join (test10)) #表示以字符串_分割字符print ("". Join (TEST10)) #以空格分割test15 = " Absdfsnlkjsljlkj "Print (Test15.partition (" s ")) #指定以s字符从左开始分割字符串, split print only once (Test15.rpartition (" s ")) # Specifies that the string is split from right to start with the S character, split only once print (Test15.split ("s", 2)) #指定以s字符从左开始分割字符串, split 2 times, and if there is no second argument, the default is all split, After the split S is not included in the segmented character print (Test15.rsplit ("s", 2)) test16 = "AJF\NADFJADFJADF\NFAJDFJJ" Print (Test16.splitlines ()) # By default, the string is split by \ n, and the split string does not contain \nprint (Test16.splitlines (True)) #默认使用 \ n is string split, and the split string contains \nprint ( Test16.splitlines (False)) #同不带参数
5, delete the space operation:
test12 = "\nalex" Print (Test12.lstrip ()) #删除左边的空格或左边的换行print (Test12.rstrip ()) #删除右边的空格 ... Print (Test12.strip ()) #删除所有的空格 ... Print ("Baihl". Lstrip ("Bax")) #从左开始匹配, delete the longest character to match, once and again, output "IHL"
6. Start and end judgment operation
Print (Strname.endswith ("I")) #表示字符串以什么结尾, output bool value print (Strname.startswith ("B")) #表示字符串以什么开始
7, Case conversion
test11 = "Alex" Print (Test11.islower ()) #判断是否全是小写print (Test11.lower ()) #全部转换成小写print (Test11.isupper ()) #判断是否全是大写print (Test11.upper ()) #全部转换成大写print (Test11.swapcase ()) #大小写转换, turn uppercase in the string to lowercase, lowercase to uppercase
8, judge the letter, number, space, underline:
Test3 = "baihl1992" Print (Test3.isalnum ()) #判断字符串中只包含数字或字符test5 = "Alex in 2" Print (Test5.isalpha ()) # Determine if the string is all characters, Chinese also count character Test6 = "123" Print (Test6.isdecimal (), Test6.isdigit ()) #判断是否是数字print (Test6.isnumeric ()) #判断是否是数字, you can determine the number of Chinese test7 = "_\n" Print (Test7.isidentifier ()) #判断是否是字母, number, or underline print (test7.isprintable ()) # Whether to include invisible characters test8 = " " Print (Test8.isspace ()) #判断字符串是否全是空格
9. Judging the title
Test9 = "Baihl up li" Print (Test9.istitle ()) #判断是否是标题 with the title representing all the words in the string with the first uppercase print (Test9.title ()) #转换成标题
10. Production Form
test4 = "Hello\tworld\tbaihl" Print (test4) print (Test4.expandtabs (6)) #表示以tab分割, the width of each tab string is 6test4 = "username\ Tmail\tpasswd\nbaihl\[email protected]\t123456\nhaoliang\[email protected]\t123456\n "Print (Test4.expandtabs (20)) #制作表格, 20 width per column
11. Other Operations
strname = "HELLO World" Print (Strname.casefold ()) #小写转换print (Strname.lower ()) #小写转换print (Strname.center (20 , "*")) #字符居中, Width 20, second parameter optional print (Strname.ljust ("*")) #左对齐print (Strname.rjust (), "*") #右对齐print ( Strname.zfill) #右对齐, use 0 fill print (Strname.count ("I")) #子字符串的个数print (Strname.count ("I", 5)) # From the 5th subscript position, look for print (Strname.count ("I", 5,6)) #设置查找的起始位置和结束位置
Python Basic data type