zoukankan      html  css  js  c++  java
  • python字符串中 r'', b'', u'', f'' 的含义

    字符串前加 r
    r"" 的作用是原始字符输出,转义字符不起作用
     
    即如果是“ ”那么表示一个反斜杠字符,一个字母r" "不是表示换行了。
    以r开头的字符,常用于正则表达式,对应着re模块。
     
    str1= 'input '
    str= r'input '
    print(str1)
    print(str)
    输出:
    input
     
    input
     
    字符串前加 f
    # 以 f开头表示在字符串内支持大括号内的python 表达式
    import time
    t0 = time.time()
    time.sleep(1)
    name = 'processing'
    print(f'{name} done in {time.time() - t0:.2f} s') 
    输出:
    processing done in 1.00 s
     
     
     
    字符串前加 b
    例: response = b'<h1>Hello World!</h1>'     # b' ' 表示这是一个 bytes 对象
    作用:
    b" "前缀表示:后面字符串是bytes 类型。
     
    用处:
    网络编程中,服务器和浏览器只认bytes 类型数据。
    如:send 函数的参数和 recv 函数的返回值都是 bytes 类型
     
    附:
    在 Python3 中,bytes 和 str 的互相转换方式是
    str.encode('utf-8')
    bytes.decode('utf-8')
     
     
     
    字符串前加 u
    例:u"我是含有中文字符组成的字符串。"
     
    作用:
    后面字符串以 Unicode 格式 进行编码,一般用在中文字符串前面,防止因为源码储存格式问题,导致再次使用时出现乱码。
  • 相关阅读:
    python全栈开发_day48_bootstrap
    python全栈开发_day47_js,jQuery
    python全栈开发_day46_html文本标签
    抽象集合
    ViewState的用法
    sql(join中on与where区别)
    微软的技术文档链接地址
    微软的帮助文档,详细的介绍VS开发工具的使用
    [摘录]解决C# DateTime去掉时分秒几种方法
    验证(摘录)
  • 原文地址:https://www.cnblogs.com/ylxtiankong/p/14951144.html
Copyright © 2011-2022 走看看