1.5.1 单引号、双引号、三引号
a = "Hello world" #双引号
b = 'python is groovy' #单引号
c = """ Computer says 'No' """ #''' ''' 三引号
print ''' content-type:text/html
<hl> Hello World </hl>
click <a href="http://www.python.org">here</a>
''' #三引号可以将多个字符放在多行
1.5.2
字符串索引和字符串切片
b = a[4]
c = a[:5] #从字符串的起始位置到第4项,不包括第5项,'Hello'
d = a[6:] #从第6项到字符串结尾,'world'
e = a[3:8] #第3项到第7项
print ("c = %s \n" %c)
print ("d = %s \n" %d)
print ("e = %s\n" %e)
输出结果:
c = "Hello"
d = "World"
e = "lo wo"
1.5.3 字符串连接:加( + )
g = a + "This is a test"
print ("g = %s \n" %g)
输出结果:"Hello world This is a test"
x = "37"
y = "41"
z = x + y #z = "3741"
print ("z = %s \n" %z)
输出结果: z = "3741"
1.5.4字符型转换整型、浮点型
z = int(x) + int(y)
print("%3d" %z)
输出结果:78
1.5.5 转换为字符串
x = 35
s1 = "This value of x is " + str(x)
s2 = "This value of x is " + repr(x)
s3 = "This value of x is " + format(x,"2d")
print("%s" %s1)
print("%s" %s2)
print("%s" %s3)
输出结果:
"This value of x is 35 "
"This value of x is 35 "
"This value of x is 35"
x = 3.4
y = str(x)
print("%s" %y)
z = repr(x)
#rint("%s" %z)