Learning and summary of string, list, tuple and dictionary methods in Python

Keywords: Python Pycharm

;

1. String

1.1 string basis

        In Python, a string is a combination of a string of characters. It is an immutable and finite sequence of characters, including visible characters, invisible characters (such as space characters) and escape characters. Python provides a large number of methods to manipulate strings through str type, such as string replacement, deletion, interception, copy, connection, comparison, search, separation, etc.

1.1.1 definition string

(1) Single line string

        Single quotation marks and double quotation marks are often used to represent single line strings. You can also add a newline character (\ n) to the string to indirectly define multi line strings.

str1="Hello world" #Use double quotation marks to define a string
str2='Hello "world"!' #Use single quotation marks to define strings (outer single and inner double)
print(str1)
print(str2)

1.1.2 value in string

Use square brackets   [ ]   To intercept the string. Variable [head subscript: tail subscript]

str1 = 'Hello World!'
str2 = "balahusdcd"
print("str1[0]: ", str1[0])
print("str2[1:5]: ", str2[1:5])

  The above implementation results:

 str1[0]:  H
 str2[1:5]:  alah

1.1.3 string splicing

(1) Use   +   connect.

name='Tom'
age='12'
he=name+age
print(he)

  Above execution result: Tom12

(2) Use   *   connect

name='Tom'
he=name*10
print(he)

  The above execution results: tomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtomtom

1.1.4 built in function

str='i am very great'
print(str.capitalize())
#This method returns an uppercase string.
print ("str.center(40, '*') : ", str.center(40, '*'))
#Returns a string centered on the specified width. If the width is less than the string width, the string is returned directly. Otherwise, fillchar(*) is used to fill in.
print ("str.count('a') : ", str.count('a'))
#This method returns the number of occurrences of the substring in the string
print (len(str))
#Return object length
print (str.swapcase())
#Returns a new string generated after case conversion
print (str.isdecimal())
#Returns True if the string contains only decimal characters, otherwise False.
print(str.title())
#Returns a "captioned" string, which means that the first letters of all words are capitalized.

2. List

The list can perform operations, including indexing, slicing, adding, multiplying, and checking members.

2.1 characteristics of the list

2.1.1 index

list=['Tom','356','Ocean','Orscar','8Leo','YEE']
#Create a list, [] for
print(list)
#Output list
print(list[1])
#Output second element, 356
print(list[-1])
#Output the last element, YEE

  The above implementation results:

['Tom', '356', 'Ocean', 'Orscar', '8Leo', 'YEE']
356
YEE

2.1.2 slicing

print(list[1:])
#Print content after the first element
print(list[:-1])
#Prints the contents before the last element
print(list[::-1])
#Reverse order output

  The above implementation results:

['356', 'Ocean', 'Orscar', '8Leo', 'YEE']
['Tom', '356', 'Ocean', 'Orscar', '8Leo']
['YEE', '8Leo', 'Orscar', 'Ocean', '356', 'Tom']

2.1.3 repeat, connect

print(list * 3)
#Output three times
list1 = ['nfs','samba']
print(list + list1)
#connect

  The above implementation results:

['Tom', '356', 'Ocean', 'Orscar', '8Leo', 'YEE', 'Tom', '356', 'Ocean', 'Orscar', '8Leo', 'YEE', 'Tom', '356', 'Ocean', 'Orscar', '8Leo', 'YEE']
['Tom', '356', 'Ocean', 'Orscar', '8Leo', 'YEE', 'nfs', 'samba']

2.1.4 for loop

print('nfs' in list)
##Judge whether it exists

for i in list:
	print(i)
#Traversal outputs each element

  The above implementation results:

False
Tom
356
Ocean
Orscar
8Leo
YEE

2.1.5 nesting

list2 = [['abc','def','www'],[1,2,3],['mike','tony','sun']]
print(list2[2][1])
#The second element of the third element
print(list2[:][1])
#Second element

  The above implementation results:
tony
[1, 2, 3]

2.2 list addition, deletion, modification and query

2.2.1 add

list=['Tom','Luck','Jack']
print(list + ['firewalld'])   
#By connecting

list.append('firewalld')   
print(list)    
#Append: append an element to the list

list.extend(['mysql','firewalld'])
print(list)
#extend: append multiple elements to the list

list.insert(1,'samba')  
print(list)
#Inserts an element at the specified index location  

  The above implementation results:

['Tom', 'Luck', 'Jack', 'firewalld']
['Tom', 'Luck', 'Jack', 'firewalld']
['Tom', 'Luck', 'Jack', 'firewalld', 'mysql', 'firewalld']
['Tom', 'samba', 'Luck', 'Jack', 'firewalld', 'mysql', 'firewalld']

2.2.2 deletion

a = list.pop(0)  
print(a)
#Pop up the first element  

list.remove('ssh') 
print(service)
#Specifies the name of the deleted object 

del list
print(list)
#Delete list


  The above implementation results:

Tom
['Jack']
<class 'list'>

2.2.3 modification

list[0] = 'mysql'
print(list)

  The above implementation results:

['mysql', 'Luck', 'Jack']

2.2.4 check

list.count('ssh')

  Above execution result: 0

3. Tuple

Elements of tuples cannot be modified. Tuples use parentheses   (), list in square brackets   [ ].

Tuple creation is simple. You only need to add elements in parentheses and separate them with commas.

Element values in tuples are not allowed to be deleted, but we can use del statement to delete the whole tuple.

4. Dictionary

A dictionary is another variable container model and can store objects of any type.

Each key value of the dictionary   key=>value   Colon for  :  Split, each pair is separated by a comma (,), and the whole dictionary is included in curly braces   {}   Yes.

Keys must be unique, but values do not.

The value can take any data type, but the key must be immutable, such as string and number.

Posted by thors1982 on Fri, 10 Sep 2021 00:57:34 -0700