2.1 variables and strings
1. Variables
Variable var, try to choose a professional name named
Variable usage rules:
# Before use variables must be assigned
# variables need not be declared, it can be assigned directly
# variable name can contain letters, numbers, underscores, but can not begin with a numeric
# uppercase and lowercase letters are different variable name
the # symbol is assigned =
teacher = "weivid"
print(teacher)
teacher = "weivid1"
print(teacher)
first = 3
second = 8
third = first + second
print(third)
operation result:
2. String
2.1 string concatenation
# Also known as text strings, different from the numbers, two are completely different
String String is spliced +
myteacher = "weivid"
yourteacher = "wang"
ourteacher = myteacher + yourteacher
print(ourteacher)
operation result:
2.2 Creating strings
Need quotes around character, may be a single or double quotation marks, must be paired
specific characters need to use the escape character \
print('5'+'8') #两个字符的拼接
print('Let\'s go')
print("Let's go") #可以使用双引号里加单引号的形式正常打印
operation result:
2.3 the original string
2.3.1 use the escape character \ escapes
2.3.2 escape r (original string raw) before the string
End of the original string can not add a backslash \
print('C:\now')
print('C:\\now')#使用反斜杠\进行转义
print('C:\\now\\uesr\\weivid\\home\\Desktop')
print(r'C:\now\uesr\weivid\home\Desktop')#对于字符串中有很多反斜杠\,可以使用在字符串r进行转义
#print(r'C:\now\uesr\weivid\home\Desktop\')error
operation result:
2.3.3 backslash end of the method
print(r'C:\now\uesr\weivid\home\Desktop\\'[:-1])
print(r'C:\now\uesr\weivid\home\Desktop' + '\\' )
print(r'C:\now\uesr\weivid\home\Desktop\ ')
operation result:
2.3.4 long string
Triple quotes "" "" "" ", '' '' ''
# long string across multiple lines
following the mini-game is to modify the code after printing
print(
"""
print("第二课\n小的游戏规则")
print("--------我是weivid_wang----------")
temp = input("猜一下weivid_wang心中想的是哪个数:")
guess = int(temp)
if guess < 8:
print ("shu")
if guess == 8:
print("卧槽你是我心中的蛔虫吗\n哼猜中了也没有奖励!!")
else:
print("猜错了,我心中的想的是8!!")
print("游戏结束,不玩啦!")
"""
)
operation result: