python数值转换字符串_python 中的 用chr()数值转化为字符串,字符转化为数值ord(s)函数...

1.1 python字符串定义

#!/usr/bin/python

# -*- coding: utf8 -*-

# 定义一个字符串

s1 = 'this is long String that spans two lines'

# 表示下面一行是上一行的延续

s2 = 'this is long String\

that spans two lines'

#原样输出字符串

s3 = """this is long String

that spans two lines

"""

# 换行输出字符串

s4 = 'this is long String\n\

that spans two lines'

print "s1=",s1

print "s2=",s2

print "s3=",s3

print "s4=",s4

输出结果:

20181207154946774.png

1.2 python字符与字符值之间的转换

A :字符转化为数值&数值转化为字符串:其中ord函数可以把字符串转化为整数的范围为【0,65535】

#!/usr/bin/python

# -*- coding: utf8 -*-

# 字符转化为数值

s = 'a'

print ord(s)

# 数值转化为字符串

b = 97

print chr(b)

B:chr()函数的参数args在【0,256】,意思就是只能把【0,256】的数字转化为字符串

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Nob3VsaXFpbmdrZTEyMw==,size_16,color_FFFFFF,t_70

C:把一个Unicode码值转化为一个Unicode字符串

#!/usr/bin/python

# -*- coding: utf8 -*-

#把unicode码值转化为字符串

print repr(unichr(8224))

#把unicode字符串转化为码值

print repr(ord(u'\u2020'))

D:chr()函数与str函数的区别:

1. chr()函数是把一个小整数作为参数,并返回对应于ASCII单字符的字符串

2. str()函数是把任何整数作为参数,返回一个该整数的文本形式的字符串

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值