Python3基础之(四)变量variable及字符串变量

一、自变量命名规则

可以将一个数值,或者字符串附值给自变量,如apple=1 中,apple为自变量的名称,1为自变量的值。 也可以将字符串赋值给自变量 apple=‘iphone7 plus’

>>> apple=1
>>> print(apple)
1
>>> apple='iphone7 plus'
>>> print(apple)
iphone7 plus

如果需要用多个单词来表示自变量,需要加下划线,如apple_2016=‘iphone 7 plus’ 请看代码

>>> apple_2016='iphone 7 plus+new mac'
>>> print(apple_2016)
iphone 7 plus+new mac

一次定义多个自变量 a,b,c=1,2,3

>>> a,b,c=1,2,3
>>> print(a,b,c)
1 2 3

二、字符串变量

基本上每一个程序中都要用到字符串,但需要注意以下几个方面:

单引号

使用单引号指定字符串,例如:
I‘m your teacher
注意单引号中的所有的空白、例如空格和制表符都按原样保留的

双引号

双引号和单引号完全一样

三重引号

可以使用三重引号("""或’’’)指定多行字符串,在三重引号中可以自由的使用单引号和双引号。例如:

a="""this is a multi-line string,this is the firstline
this is the second line
this is the thrid line
"""
print(a)

注意:
python中没有单独的“char”(字符型)数据
python字符串是不可以改变的

格式方法

当需要从其他信息构建字符串的时候就可以使用format()方法,例如:

name="hupo"
age=28
print("{0} is very handsome,his age is {1}".format(name,age))

输出:

hupo is very handsome,his age is 28

你可能感兴趣的:(Python基础,Python基础)