1. Variable naming and use
variable naming rules : A. Variable names can only contain (alphanumeric underscores), and variables cannot start with a number, for example: variable s_1 (correct), variable 1_s (error)
B. Variable names cannot contain spaces, you can use underscores to interval
C. Variables cannot be named with keywords and function names, for example: print
d. Variable names should be short and identifiable. (see the variable name to know what it might mean)
E. Avoid the use of easily confusing letters and numbers, such as 0 and 1,o and I (when the code is very large, this small error is not easy to find, try not to dig a hole for yourself)
2. Simple data types
String string: Use single quotation marks in Python or use double quotation marks to amplify strings.
For example: str = "Qazwsxedc" with str = ' Qazwsxedc ' means the same string, with the same effect.
Basic operation of String:
A. the case of the first letter of the string modified , using the method: string name. Title (), which is called the title method for example:
' Qazwsxedc '>>> str.title ()'qazwsxedc'
B. the case of all characters in the string , call the upper and lower methods, respectively. Upper () is to make all strings uppercase, lower the opposite. For example:
>>> str.upper ()'qazwsxedc'>>> str.lower () ' Qazwsxedc '
C concatenation of strings , using the plus sign for stitching, if required, a space string with two strings in the middle. For example:
>>> str1 = " adu " >>> str2 = " you Has to more harder!! " >>> str3 =str1 + str2 >> > STR3 " Aduyou has to more harder!! " >>> STR3 = str1 + " " + str2 >>> STR3 " adu you had to more harder!! " >>>
D. string interception , intercept part of the string: variable [head subscript: Tail subscript] (Note: The index value is starting from 0, 1 means starting from the tail, the output is the first (tail subscript), not (tail subscript) plus one. For example:
STR3 ='ADU Harder!!!'Print(STR3)Print(Str3[0:7])#starts from 0 to the end of the 7th character, including the space characterPrint(Str3[:-1])#-1 means starting from the tail and not writing the default from the beginning, that is, from the beginning to the second end of the last characterPrint(str3[2:])#output A third character laterPrint(STR3 * 2)#output String two times
#运行结果
ADU Harder!!!
Adu You
Adu harder!!
U has to work more harder!!!
ADU Harder!!! ADU Harder!!!
>>
Python learns-------variables and simple data types (String)