So let's set up a python file , be known as hello_world.py, Let's enter a piece of code first , use python To run it .
print("hello python world")
Run the above code , We'll see the following output :
hello python world
So we're done python The first line of code outputs , Now let's try in hello_world.py Use a variable in the , The contents are as follows :
message = "hello python world"
print(message)
Run this program , We see the output :
hello python world
The output is the same as before , This is where we added a new one called message The variable of , The value stored in the variable is “hello python world”, Same value as the previous output , The creation of variables only provides a temporary storage space for text , It can simplify the procedure , Make the program more readable , More understanding . In the program, the value of a variable can be changed at any time , and python The latest value of the variable will always be recorded . for example :
message = "hello python world"
print(message)
message = "how are you"
print(message)
Run this program , as follows :
hello python world
how are you
According to the above example , We can see : In the program, the value of a variable can be changed at any time , and python The latest value of the variable will always be recorded .
When we use variables to name them , There are also rules and guidelines to follow , Breaking these rules will lead to errors , The guidelines make the code you write easier to understand and read . Like any other language , The following rules apply to variable naming :
A string is a series of characters . stay Python in , What is caused by parentheses is a string . The quotation marks can be single quotation marks or double quotation marks , as follows :
"this is a string"
'this is also a string'
For strings , The simplest operation you can perform is to change the case of words in it , The following describes how to change the case of strings .
String words are capitalized :title()
word = "love you"
print(word.title())
Run this program , Output is as follows :
Love You
It can be seen that , The first letter of both words in the string is capitalized , therefore title() The function of the is to display each word in capital letters .
Change the string to all uppercase or all lowercase :upper(), lower()
word = "love you"
print(word.upper())
print(word.lower())
Run the code , Output is as follows :
LOVE YOU
love you
therefore ,upper() Change all strings to uppercase , and lower() Change all strings to lowercase , The two functions are just the opposite .
In most cases , We need to merge strings , stay Python We Use plus sign (+) Merge strings :
first_word = "love"
last_word = "you"
full_word = first_word + '' + last_word
print(full_word)
function , Output :( Spaces enclosed in single quotation marks are used to separate the combined two characters )
love you
This method of combining strings is called concatenation . By joining together , You can use information stored in variables to create complete information :
first_word = "love"
last_word = "you"
full_word = first_word + '' + last_word
print("I " + full_word.title())
function , Output :
I Love You
We can create and output a complete message by string merging , You can also put the spliced whole message in a variable .
To add tabs to a string , Character combinations can be used \t , To add a newline character to a string , Character combinations can be used \n.
The specific use will not be introduced here
Python Be able to find extra white space at the beginning and end of a string , want Make sure there is no white space at the end of the string , Serviceable method rstrip().
>>> favorite_language = "python "
>>> favorite_language
'python '
>>> favorite_language.rstrip()
'python'
>>> favorite_language
'python '
We can see that , Deleting spaces is only temporary , To permanently delete whitespace in a string , The result of the delete operation must be saved back to the variable :
>>> favorite_language = "python "
>>> favorite_language = favorite_language.rstrip()
>>> favorite_language
'python'
Remove the blank space at the beginning of the string lstrip() , Remove whitespace at the end of the string rstrip(), Delete whitespace at both ends of the string strip().
In programming , You often need to change the value of a variable , Save the new value back into the original variable , This is why the value of a variable may change as the program runs or the user enters data .
Using functions str() Avoid typos : Call function str(), It makes Python Represents a non string as a string .
View keywords
import keyword
keyword.kwlist
Variable assignment
Keyed assignment :name = user = 'petter'
Sequence unpacking assignment :name, age = 'jake', 21
You can use sequence unpacking assignment to exchange values between variables
a = 10
b = 5
a, b = b, a
Variable : To save things , Carrying data ,
in other words , When a constant pointed to by an identifier is replaced by another constant , The original constant will no longer occupy memory .