Python suitable for network siege lion learning-basic grammar (data type-string)

To help the company interview two network siege lion girls, we first asked if there was a boyfriend, the first said yes, and then we asked her to mobilize an OSPF structured network, and I depended on her to mobilize it! ! Then we asked her to prove the spf algorithm again using mathematical methods, and she ran away in anger. The second one said not yet. We asked her to have a static router. The girl said that she would not... Finally, we said to the girl together: It’s okay, we teach you, you come to work this afternoon!

Insert picture description here

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.


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 .
Insert picture description here

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
Insert picture description here

Enter the variable name directly in the interpreter to get its value, which is not possible in script mode.
Insert picture description here
In Python, we can also use the plus sign + to concatenate strings

Insert picture description here

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.

Insert picture description here
a and b are strings, q=24 is an integer error, the solution: the str() function converts q from an integer to a string
Insert picture description here

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.
    Insert picture description here

3. Methods and functions related to strings

1、upper()

Convert lowercase letters in a string to uppercase letters
Insert picture description here

2、lower()

Convert uppercase letters in string 1 to lowercase letters
Insert picture description here

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.
Insert picture description hereInsert picture description here

4、count()

Determine how many characters or numbers there are in a string.
Insert picture description here

5 、 len ()

Determine the length of the string.
Insert picture description here

6. List

A list is an ordered collection, represented by [], and the data in the collection is called an element.
Insert picture description hereUse the index to access and specify the elements in the list, the order of the index starts from 01.

Insert picture description here

7、input()

Prompt the user to enter data to interact with Python.
Insert picture description here

Insert picture description here

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).
Insert picture description here

10、endswith()

Determine whether the content of the string ends with the given string, and the return value is a Boolean value (True, False).

Insert picture description here

11、isdigit()

To determine whether the content of the string is an integer, the return value is a Boolean value (True, False).
Insert picture description here

12 、 isalpha ()

Determine whether the content of the string is an English letter, and the return value is a Boolean value (True, False).

Insert picture description hereIn the isalpha() function, all non-English letters appearing in the string content will return False.

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44309905/article/details/114846940