Python learning-Basic Python data types (1), python learning Data Types
Python3 Basic Data Type
Variables in Python3 do not need to be declared. Each variable must be assigned a value before it can be used.
Python3 has six standard data types: Number, String, list, Tuple, Dictionary (Dict), and set)
Number (Number ):
Python3 supports int, float, bool, and complex (plural)
The type () function can view the object type referred to by the variable.
String (String ):
Strings in Python are enclosed by single quotation marks (') or double quotation marks ("), and special characters are escaped using a backslash.
Note: '''... ''' is of no special use when creating a short string. It is mainly used to create multi-line strings, as shown in the following example:
>>> Poem = ''' There was a Young Lady of Norway,
... Who casually sat in a doorway;
... When the door squeezed her flat,
... She exclaimed, "What of that? "
... This courageous Young Lady of Norway .'''
Python also allows null strings and does not contain any characters. It is completely legal!
Conversion between numbers and strings:
Convert a string to a number:
>>> Int ('20 ')
20
>>> Float ('20 ')
20.0
>>> Int (20)
20
Convert a number to a string:
>>> Str (20)
'20'
>>> Str (20.0)
'20. 0'
>>> Str (True)
'True'
Use + stitching
In python, you can try and splice multiple strings or string variables.
>>> 'Release the Tom! '+' At once! '
Release the Tom! At once!
Use [] to extract characters
Add [] to the end of the string and add an offset to [] to retrieve the string at this position. The offset of the first character is 0, the second is 1, and so on. The first offset on the right is-1, the second offset is-2, and so on from right to left...
>>> Str = 'abcdefghijklmnopqrstuvwxy'
>>> Str [0]
'A'
>>> Str [-1]
'Z'
>>> Str [3]
'D'
The string is immutable. Sometimes the string needs to be combined to use some string functions: replace () and multipart operations.
>>> Name = 'henny'
>>> Name. replace ('h', 'P ')
'Penny'
>>> 'P' + name [1:]
'Penny'
Use [start: end: step] To split:
Sharding: You can extract substrings from a string. Start offset, end offset, and optional step to define a shard
1. [:] extract the entire string from the beginning to the end
2. [start:] extract from start to end
3. [: end] extract from the beginning to end-1
4. [start: end] extract from start to end-1
5. [start: end: step] extracts one character from start to end-1.
>>> Str = 'qwertyuiop'
>>> Str [:]
'Qwertyuiop'
>>> Str [5:]
'Yuiop'
>>> Str [-3:]
'Iop'
>>> Str [:-3]
'Qwertyu'
>>> Str [-6:-3]
'Tyu'
The step size is 3.
>>> Str [: 3]
'Qrule'
Returns a string by using slices.
>>> Str [:-1]
'Heiuytrewq'
Other common operations on strings:
>>> Str = 'qwertyuiop'
Calculate the length of a string
>>> Len (str)
10
Use split () to separate:
You can use the built-in string function split () to split a string into a list composed of several substrings Based on the delimiter.
A list is a series of values separated by commas (,). The list is enclosed by square brackets.
>>> Todos = 'get gloves, get mask, give cat vitamins, call ambulance'
>>> Todos. split (',')
['Get gloves ', 'Get Mask', 'Give cat vitamins', 'Call ambulance']
In the preceding example, the string name is todos, the function name is split (), the input parameter is a single separator split (), and the input parameter is a single separator ','.
If no Delimiter is specified, split () uses the blank characters by default-line breaks, spaces, and tabs.
>>> Todos. split ()
['Get', 'gloves, get', 'mask, give', 'cat', 'vitamins, call', 'Ambulance ']
Use join () to merge:
As you may have guessed, the join () function is the opposite of the split () function: it splits the list containing several substrings, and combine these substrings into a complete large string. Join ()
>>> Crypto_list = ['eti', 'BigFoot', 'loch Ness monster']
>>> Crypto_string = ','. join (crypto_list)
>>> Print ('found and signing book deals: ', crypto_string)
Found and signing book deals: Yeti, Bigfoot, Loch Ness Monster
Replace with replace:
>>> Str = 'qwertyuiop'
>>> Str. replace ('w', 'ooooo ')
'Qoooooertyuiop'
Up to three
>>> Str = 'qwerwerwtytewwiitw'
>>> Str. replace ('w', 'oooo ', 3)
'Qooooerooerooooerwtytewwiitw'
Calculates the number of times that 'W' appears in the string.
>>> Str. count ('W ')
7