字符串类型
数据类型¶
name = "itcast"
print(name)
print(type(name))
itcast
<class 'str'>
Note
字符串类型描述为str
一些案例¶
字符串包裹方式¶
# 单引号
name = 'itcast'
# 双引号
name = "itcast"
单引号和双引号混用¶
words = '张三说:"你好",李四回答到:"我很好"'
print(words)
words = "I'm fine"
words = '小明说:"老师, \'传智播客\'怎么写"'
乘法¶
words = "hello"
words = words * 10
print(words)
占位符¶
符号 | 描述 |
---|---|
%d |
格式化整数 |
%s |
格式化字符串 |
%f |
格式化小数 |
格式如下:
" [符号占位...]" % (值...)
符合案例¶
# 整数
worlds = "age = %d" % 10
print(worlds)
# 小数
worlds = "age = %f" % 10
print(worlds)
# 多个占位符
worlds = "name is %s, age is %d" % ('itcast', 10)
print(worlds)
Format格式化¶
worlds = "name is {}, age is {}".format("itheima", 10)
print(worlds)