String introduction
-
Think about it:
When calling the browser to log in to some websites, you need to enter a password. After the browser transmits the password to the server, the server will verify the password. The verification process is to compare the previously saved password with the password passed this time. , if they are equal, then the password is considered correct, otherwise it is considered incorrect; since the server wants to store these passwords, it can use a database (such as MySQL), of course, for the sake of simplicity, we can first find a variable to store the password; then how? What about storing passwords with letters?
-
answer:
string
<1> Format of strings in python
The variable a defined as follows stores the value of the numeric type
a = 100
The variable b defined as follows stores a value of type string
b = "hello itcast.cn" or b = 'hello itcast.cn'
Small summary:
- The data in double quotes or single quotes is a string
<2> String output
name = ' xiaoming ' position = ' Programming ' address = ' Beijing basement ' print ( ' --------------------------------------------------------- --- ' ) print ( " Name: %s " % name) print ( " Position: %s " % position) print ( " Company address: %s " % address) print ( ' -------- ------------------------------------------ ' )
operation result:
--------------------------------------------------
Name: xiaoming
Position: Programmer
Company address: Beijing basement
--------------------------------------------------
string input
Note: The data obtained by input is saved as a string, even if the input is a number, it is also saved as a string
demo:
userName = input( ' Please enter the user name: ' ) print ( " User name: %s " % userName) password = input( ' Please enter the password: ' ) print ( "The password is: %s " %password)
Running result: (The result is different depending on the input)
Username: soaeon
Username: soaeon
Please enter your password: hhhhh
The password is: hhhhh
Subscripting and Slicing