zoukankan      html  css  js  c++  java
  • python 中的 用chr()数值转化为字符串,字符转化为数值ord(s)函数

    1.1 python字符串定义

    1. #!/usr/bin/python
    2. # -*- coding: utf8 -*-
    3. # 定义一个字符串
    4. s1 = 'this is long String that spans two lines'
    5. # 表示下面一行是上一行的延续
    6. s2 = 'this is long String
    7. that spans two lines'
    8. #原样输出字符串
    9. s3 = """this is long String
    10. that spans two lines
    11. """
    12. # 换行输出字符串
    13. s4 = 'this is long String
    14. that spans two lines'
    15. print "s1=",s1
    16. print "s2=",s2
    17. print "s3=",s3
    18. print "s4=",s4

    输出结果:

      

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

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

    1. #!/usr/bin/python
    2. # -*- coding: utf8 -*-
    3. # 字符转化为数值
    4. s = 'a'
    5. print ord(s)
    6. # 数值转化为字符串
    7. b = 97
    8. print chr(b)

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

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

    1. #!/usr/bin/python
    2. # -*- coding: utf8 -*-
    3. #把unicode码值转化为字符串
    4. print repr(unichr(8224))
    5. #把unicode字符串转化为码值
    6. print repr(ord(u'u2020'))

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

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

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

  • 相关阅读:
    【python笔记】类
    【Marva Collins' Way】第八章
    【Marva Collins' Way】第七章
    【python笔记】包
    【python笔记】模块
    【Marva Collins' Way】第六章
    【Marva Collins' Way】第九章
    【python笔记】异常
    Axios跨域&封装接口
    js更新数据
  • 原文地址:https://www.cnblogs.com/presleyren/p/11365816.html
Copyright © 2011-2022 走看看