1. Variable naming specification
- Variable names can contain only letters , numbers , and underscores . Variable names can start with a letter or underscore , but not with a number . For example, Variable_1 is legal, but 1_variable is an illegal variable name.
- Variable names cannot contain spaces , but you can use underscores to split words. For example, Hello_world is legal, but Hello world is illegal.
- You cannot use a keyword and a function name as a variable name .
- Variable names should be both brief and descriptive.
- Be careful to use letters and numbers that are easily confused. For example, the letter L and the number 1, the letter O and the number 0.
2. String
A string is a series of characters. In python, strings are enclosed in quotation marks (single quote ' ... ', double quotes "...").
2.1. Modify String case
" Ada Lovelace " Print (Name.title ()) # title () displays each word in the string in the first-letter capitalization.
" Ada Lovelace " Print # the output is all uppercase Print (Name.lower ()) # the output is all lowercase
2.2. Merging (splicing) strings
1First_Name ="Ada"2Last_Name ="Lovelace"3Full_name = first_name +" "+ last_name#use + to merge first_name, spaces, and last_name4 5 Print("Hello,"+ full_name.title () +"!" )6 7 " "message = "Hello," + full_name.title () + "!"8 print (message)" "
The connector "+" here creates a new memory space in memory, so avoid this usage.
2.3, tab, line break
Whitespace refers to any nonprinting character, such as spaces, tabs, and line breaks. \ t is a tab character, \ n is a newline character.
2.4. Delete blank
1Language ='Python'2Language#' Python '3Language#' Python '4 5Language.rstrip ()#' Python '. The Rstrip () method can remove the trailing space, but only temporarily, the value of language is not changed. 6Language#' Python '. The spaces are still in. 7 8Language = Language.rstrip ()#to permanently delete whitespace in a string, you can save the result in a variable. 9Language#' Python 'Ten One #the Lstrip () method removes spaces to the left of the string, and #strip () method to remove spaces around the string.
3. Digital
Common integer, floating-point type. For the result of the operation of floating-point numbers, the number of decimal digits may be indeterminate.
3.1 Using the function str () to avoid type errors
1Age = 232msg ="Happy"+ Age +"Rd Birthday"3 4 Print(msg)#will report the ' age ' type error. 5 6 #use the following method to avoid this error7 8Age = str (23)#converts 23 of the int type to a string, processed in characters 2 and 3. 9msg ="Happy"+ Age +"Rd Birthday"Ten One Print(msg)
01-python-variables and data types