python笔记03:数字类型string

变量类型

标准数字类型六种

  • 数字 number
  • 字符串 string
  • 列表 list
  • 元组 tuple
  • 字典 dict
  • 集合 set

字符串

  • 表达文字信息的内容,比如"马露源小可爱"
  • 形式是双引号(" ")引起来的一段内容
  • 引号包括
    • 单引号 ’
    • 双引号 "
    • 三引号 “”"
    • 单双引号含义一致,只能表示一行信息;三引号用来表示多行信息
  • 转义字符
  • 格式化
  • 内建函数(后延)
#字符串类型
s1 = 'I love maluyuan'
print(s2)

s2 = "I love maluyuan"
print(s1)

s3 = '''
    天上悬明月,
    清辉照万方。
    浮云虽暂避,
    终不灭清光。
'''
print(s3)

I love maluyuan
I love maluyuan

    天上悬明月,
    清辉照万方。
    浮云虽暂避,
    终不灭清光。

None

  • 表示什么都没有
  • 如果函数没有返回值,可以返回 None
  • 用来占位置
  • 用来接触变量绑定

转义字符

  • 用一个特殊的方法表示出一系列不方便写出的内容,比如回车键,换行键,退格键
  • 借助反斜杠字符,一旦字符串中出现反斜杠,则反斜杠后面一个火几个字符表示已经不是原来的意思了,进行了转义
  • 在字符串中要特别注意反斜杠 \ ,可能由转义字符出现
  • 不同系统对换行操作有不同的表示
    • windows: \n
    • Linux: \r\n
# 如果想表示出Let‘s go来
# 1. 可以使用嵌套引号,即外层使用双引号
# 2. 转义字符
s = "Let's go"
print(s)

#\' == '
ss = 'Let\'s go'
print(ss)

# \\ = \
sss = "c:\\user"
print(sss)

# 回车换行符
s1 = "I love \r\n maluyuan"
print(s1)
Let's go
Let's go
c:\user
I love 
 maluyuan
# 单个“\”的换行符用法
# 在python中,单个“\”代表此行未结束,在下一行继续
# 理论上应该写成 def myDemo(x, y, z):
def myDemo(x,\
           y,\
           z):
    print("{0}+{1}+{2}={3}".format(x, y, z, x+y+z))

myDemo(1, 2, 3)
1+2+3=6

字符串的格式化

  • 把字符串按照一定格式打印或者填充
  • 格式化由两种方法
    • 利用百分号(%)
    • 利用format函数

利用百分号格式化

  • 在字符串中,利用%表示一个特殊的含义,表示对字符进行格式化
  • %d: 此处应该放入一个整数
  • %s: 表示此处应该放入一个字符串
s = "I love %s"
name = "maluyuan"

print(s)

print(name)

print("I love %s"%"maluyuan")

print(s%"maluyuan")

print(s%name)
I love %s
maluyuan
I love maluyuan
I love maluyuan
I love maluyuan
s = "I am %d years old"

age = 22

print(s)

print (age)

print(s%22)

print("I am %d years old"%22)

print("I am %d years old"%age)

print(s%age)
I am %d years old
22
I am 22 years old
I am 22 years old
I am 22 years old
I am 22 years old
s = "I love %s, i am %d years old"
print(s)

# 注意以下表达的出错原因
# 如果字符串中由占位符,则由几个占位符必须用几个实际内容代替,或者一个也不要
#print(s%"maluyuan")

# 如果出现多个占位符,则相应内容需要用括号括起来
print(s%("maluyuan", 22))
print(s%(name,age))
I love %s, i am %d years old
I love maluyuan, i am 22 years old
I love maluyuan, i am 22 years old
# 数字的格式化例子
s = "I am %d kg and %d meter height"
print(s%(72.45, 1.81))

#
s = "I am %f kg and %f meter height"
print(s%(72.45, 1.81))

#
s = "I am %.2f kg and %.2f meter height"
print(s%(72.45, 1.81))
I am 72 kg and 1 meter height
I am 72.450000 kg and 1.810000 meter height
I am 72.45 kg and 1.81 meter height

format函数格式化字符串

  • 直接用format函数进行格式化
  • 推荐使用这种方法
  • 在使用上,以{}和:代替%号,后面用format带参数完成
name = "maluyuan"
s1 = "I love {}".format("maluyuan")
s2 = "I love {}".format(name)  
print(s1)
print(s2)
I love maluyuan
I love maluyuan
# 不指定位置 按顺序读取
print("{} {}".format("Hello", "World"))

# 指定位置 从0开始
print("{1} {0}".format("Hello", "World"))
Hello World
World Hello
age = 22
s3 = "I am {} years old".format(age)
print (s3)
I am 22 years old
s4 = "I am {1} years old and I love {0},{0} is a very beautiful girl. ".format(name,age)
print(s4)
I am 22 years old and I love maluyuan,maluyuan is a very beautiful girl. 
# 数字的格式化
# 保留小数点后两位
print( "{:.2f}".format(3.1415926) )
print( "{:.2f}".format(-3.1415926) )

# 带符号保留小数点后两位
print( "{:+.2f}".format(3.1415926) )
print( "{:+.2f}".format(-3.1415926) )

# 不带小数 四舍五入
print( "{:.0f}".format(3.1415926) )

# 数字补全 (以“0”填充左边, 宽度为2)
print( "{:0>2d}".format(5) )

# 数字补全 (以“*”填充右边, 宽度为4)
print( "{:*<4d}".format(15) )

# 以逗号分隔的数字格式
print( "{:,}".format(1000000000) )

# 百分比格式
print( "{:.2%}".format(1.25) )

# 指数记法
print("{:.2e}".format(123456789))

# 右对齐 默认 宽度为10
print("{:>10d}".format(123))
print("{:10d}".format(123))

# 左对齐
print("{:<10d}".format(123))

# 居中对齐
print("{:^10d}".format(123))
3.14
-3.14
+3.14
-3.14
3
05
15**
1,000,000,000
125.00%
1.23e+08
       123
       123
123       
   123    
# 进制
print( "{:b}".format(11) )
print( "{:d}".format(11) )
print( "{:o}".format(11) )
print( "{:x}".format(11) )

print( "{:#b}".format(11) )
print( "{:#d}".format(11) )
print( "{:#o}".format(11) )
print( "{:#x}".format(11) )
print( "{:#X}".format(11) )
1011
11
13
b
0b1011
11
0o13
0xb
0XB
# 使用命名参数
s = "我是{name},我的年龄是{age},我的身高是{height}"
print(s)
print(s.format(name="Albert",\
               age="22", \
               height="181"))

# 通过字典设置参数 需要使用“**”解包
dictionary = {"name":"Albert", "age":"22", "height":"181"}
print(s.format(**dictionary))
我是{name},我的年龄是{age},我的身高是{height}
我是Albert,我的年龄是22,我的身高是181
我是Albert,我的年龄是22,我的身高是181
#使用大括号“{}”来转义大括号
print ("{} 对应的位置是 {{}}".format("runoob"))
runoob 对应的位置是 {}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值