For network engineers, the commonly used data types are String, Integer, List, Dictionary, Float, and Boolean. The less commonly used data types are Set, Tuple, and Null.
String
Strings are text, which can be represented by single quotes'', double quotes "" and triple quotes ``''''.
One, single quotes and double quotes
When expressing a short string, single quotes and double quotes are commonly used and their usage is the same. Compare'cisco' and'huawei'. It should be noted that single quotes and double quotes cannot be mixed .
The string Juniper mixed single quotes and double quotes when assigning values, causing the interpreter to report an error.
Use the print statement to print out 3 variables
Enter the variable name directly in the interpreter to get its value, which is not possible in script mode.
In Python, we can also use the plus sign + to concatenate strings
When using plus sign + splicing variables, if one of the variables is a string, then all other variables to be spliced with it must also be strings, otherwise an error will be reported.
a and b are strings, q=24 is an integer error, the solution: the str() function converts q from an integer to a string
Two and three quotation marks
String in triple quote form:
- It is used to represent longer texts and line breaks\n are required in the text.
- Flags such as MOTD, which are used in routers and switches to warn unauthorized users of the consequences of illegally accessing the device, are configured and need to be line-wrapped.
3. Methods and functions related to strings
1、upper()
Convert lowercase letters in a string to uppercase letters
2、lower()
Convert uppercase letters in string 1 to lowercase letters
3、strip()
Remove the specified characters at the beginning and end of the string. If no parameters are specified, all spaces and newlines \n at the beginning and end of the string will be removed by default.
4、count()
Determine how many characters or numbers there are in a string.
5 、 len ()
Determine the length of the string.
6. List
A list is an ordered collection, represented by [], and the data in the collection is called an element.
Use the index to access and specify the elements in the list, the order of the index starts from 01.
7、input()
Prompt the user to enter data to interact with Python.
8、split()、join()
split() converts a string into a list, join() converts a list into a string.
I will not give an example here, the following articles will show the application of these two functions in actual combat.
9、startswith()
Determine whether the content of the string starts with the given string, and the return value is a Boolean value (True, False).
10、endswith()
Determine whether the content of the string ends with the given string, and the return value is a Boolean value (True, False).
11、isdigit()
To determine whether the content of the string is an integer, the return value is a Boolean value (True, False).
12 、 isalpha ()
Determine whether the content of the string is an English letter, and the return value is a Boolean value (True, False).
In the isalpha() function, all non-English letters appearing in the string content will return False.