Python: String, list, tuple, dictionary

Source: Internet
Author: User
Tags extend string methods

string:

All methods do not modify the value of the string, the string is the original value, but can be re-assigned; Use string methods to return a value

To use a looping string:

String method:

Name= "Hello World"
Print (Name.capitalize ()) #首字母大写
Print (Name.center, '-') #50个-put name in the middle
Print (Name.endswith (' U ')) #是否以x结尾, yes returns true
Number of print (Name.expandtabs ()) #补 \ t
Print (Name.find (' n ')) #查找字符串的索引, as with index usage, finds the return index and cannot find the return-1
Print (Name.format (name= ' Niuniu ', age=18)) #这个是格式字符串, and the first section of the blog has been written
Print (Name.format_map ({' name ': ' Niuniu ', ' age ':)) #这个也是格式化字符串, followed by a dictionary, the dictionary will be written in the back
Print (' abA123 '. Isalnum ()) #是否包含数字和字母
Print (' AbA '. Isalpha ()) #是否是英文字母
Print (' 122 '. IsDigit ()) #是否是整数
Print (' AA '. Isidentifier ()) #是否是一个合法的变量名
Print (' AA '. Islower ()) #是否是小写字母
Print (' AA '. Isupper ()) #是否是大写字母
Print (' LoadRunner book '. Istitle ()) #是不是一个标题 to determine if the first letter is capitalized
Print (' + '. Join ([' hehe ', ' haha ', ' ee ')) #拼接字符串, the elements of a reusable object (a string list dictionary, etc.) are stitched together with a single character
Print (Name.lower ()) #变成小写
Print (Name.upper ()) #变成大写
Print (' \nmysql \ n '. Lstrip ()) #默认去掉左边的空格和换行
Print (' \nmysql \ n '. Rstrip ()) #默认去掉右边的空格和换行
Print (' \nmysql \ n '. Strip ()) #默认去掉两边边的空格和换行
"MySQL". Strip (m), remove the M in MySQL
p = Str.maketrans (' ABCDEFG ', ' 1234567 ') #前面的字符串和后面的字符串做映射
Print (' CC AE GG '. Translate (p)) #输出按照上面maketrans做映射后的字符串
new_p = Str.maketrans (' 1234567 ', ' ABCDEFG ')
Print (' CC AE GG '. Translate (new_p))
Print (' MySQL is db. '. Replace (' MySQL ', ' Oracle ', 1)) #替换字符串, 1 represents the replacement of the first, does not specify the number of default replacements for all strings
Print (' MySQL is IsDB '. RFind (' is ')) #返回最右边字符的下标
Print (' 1+2+3+4 '. Split (' + ')) #切割字符串, returns a list. 1: Splits the string according to the specified string; 2: If there is nothing in the split brackets, divide by space
Print (' 1+2+3\n1+2+3+4 '. Splitlines ()) #按照换行符分割
Print (' Abcdef '. Swapcase ()) #大小写反转

Add:

Import string

Print (string.ascii_letters) #输出所有的大小写字母

Print (string.ascii_lowercase) #输出所有的小写字母

Print (string.ascii_uppercase) #输出所有的大写字母

Print (string.digits) #输出0到9的数字

list:

Lists are mutable variables

modifying values by subscript

Add value to list:

Adds an element from the specified location, and 0 represents the addition from the first element.

To delete a value from a list:

Del names deleting variables

Names.remove ("Yuansu"), remove removes the element from the list, POPs deletes the subscript of the pass

List method:

Only Pop has a return value

Sort

Nums.sort (): Sort nums list, default ascending
Nums.sort (reverse=true): Descending.
Reverse
Nums.reverse (): Reverse the value of the list

Count

Take the last value in the list: the subscript is-1, which represents the last element, 2 for the penultimate, and so on

Index

list Merge : Append extend differences:

Two listings a1,a2:a1.extend (A2): Add A2 elements to A1, A1 or one-dimensional arrays

A1.append (A2) adds the A2 array to the A1 as an element and A1 into a two-bit array

Multidimensional Arrays:

Take value: Get is Sex

slices:

Take a few elements out of a list or string

EG1: List: names=[' as ', ' SD ', ' DF ']
Print (Names[0:3]) #取第一和第二个元素, only take 0 and 1 subscript, Gu Tou regardless of the tail
Print (names[:]) #取所有的元素
Print (Names[:n]) #从第一个元素开始取到第n-1 elements
Print (names[n:]) #从n元素取到最后一个元素
Print (NAMES[A:B:C]) #从a取到b-1 elements, every C to take one time, do not write C by default one to take one, C is negative, the value is inverted, and the function of the reverse method, such as names[::-1], indicating the first value from the countdown to take the first , such as Names[8:1:-2]
EG2: string: name2= ' DFHID,DWD '
method, same as List

meta-group:

The value of the tuple cannot be changed, once created, it can no longer change the type is a tuple, the tuple is defined in (), the parentheses, the tuple has only two methods, that is, count and index, use the same list

tp= (1,2,3f,3,33re)

Print (Tp.count (3))

Print (Tp.index (3))

When defining tuples, if there is only one element, add commas, otherwise it will be considered str type. eg:a= (' abc ',)

Dictionary:

The dictionary is a Key-value data type, the dictionary definition uses {}, curly braces, each value is separated by ",", key and value are delimited with ":", key cannot be duplicated, and value is obtained by key

Two methods of value-taking:

Print (dic[' id ') and print (Dic.get (' id '))

Difference: 1, the value of the square brackets, cannot find the key value will be error, get value can not find no error, will return none. 2,get method can also pass a parameter, if get not to the key value, return parameters, such as print (Dic.get (' id ', ' Canshu ')), do not write default return None

Add key:

dic[' id ']=3
Dic.setdafault (' addr ', ' Beijing ')

Modify Key:

dic[' id ']=5 #同增加方式一样

Delete:

del dic[' id ']
Dic.popitem () #随机删除一个元素
Dic.pop (' id ') #字典是无序的, pop delete must pass a key value
Dic.clear () #清空字典

Built-in methods:

Print (All.keys ()) #返回所有的key
Print (All.values ()) #返回所有的value
Print (All.items ()) #返回所有的key和value, returns the Dict items type, in the loop, for example: for k,v in All.items ()
Print (k,v) #循环出所有的key和value, but because it is a dict_items type, the less efficient and efficient loop is: for the K in info:
Print (K,info[k]) #这样也是取所有的key和value, high efficiency
Info.update (Info2) #把两个字典合并, if you have the same key, take Info2 's value

To force type conversions:

Int () #转换成整型

Float () #转换成浮点型

STR () #转换成字符串

List () #转换成列表

Tuple () #转换成元组

Eval (' {' {' username ': 1} ') #用它来把字符串转成字典, the string must conform to the format of the dictionary

Python: String, list, tuple, dictionary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.