Basic data types and common methods in Python
Common data types in Python are: string (str), list, tuple (tuple), dictionary (DITC), collection (set)
(a) string
Common methods of strings:
1, index, the character of the corresponding position is output by subscript.
eg:i = ' Hello World '
Print (i[3]) Result: ' L '
2, Length (len), gets the length of a string.
eg:i = ' Hello '
Print (len (i)) Results: 5
3, remove whitespace (strip), remove whitespace (spaces) on both sides of the string.
eg:i = ' Hello '
new_i = I.strip ()
Print (new_i) results: Hello
4, Split (split), divides a string into two parts.
eg:i = ' HelloWorld '
new_i = I.split (' ll ')
Print (new_i) Result: [' he ', ' Oworld ']
5, slices, gets a part of the string.
i = ' HelloWorld '
Print (I[1:5]) Result: ' Ello '
It is important to note that the subscript in Python starts at 0.
(ii) List
List of commonly used methods:
1, index 2, Slice 3, append 4, delete 5, length 6, loop 7, inclusive
(iii) tuples
1, index 2, slice 3, length 4, loop 5, including
(d) Dictionaries
1, index 2, add 3, delete 4, length 5, loop
(v) Collection
1, add 2, delete 3, length 4, loop
Python beginner day1--(basic data types and common methods in Python)