python字符串前加r、f、u、l 的区别

Python探索牛 2021-01-23 17:12:16
Python 区别 字符 字符串


f-strings 是指以 f 或 F 开头的字符串,其中以 {} 包含的表达式会进行值替换。(目前支持python3.6版本)

下面看下 f-strings 的使用方法

基本使用(作用:替换值)

>>>name = 'xiaoming'
>>> age = 18
>>> f"hi, {name}, are you {age}"
#结果如下
'hi, xiaoming, are you 18'
>>> F"hi, {name}, are you {age}"
'hi, xiaoming, are you 18'

在字符串前加r可防止字符串转义

作用:没有转义特殊或不能打印的字符。

#Python学习交Q群:778463939
>>> s='abc\nabc'
>>> s
'abc\nabc'
>>> print s
abc
abc
>>> s=r'abc\nabc'
>>> s
'abc\\nabc'
>>> print s
abc\nabc

u/U:表示unicode字符串

不是仅仅是针对中文, 可以针对任何的字符串,代表是对字符串进行unicode编码。
一般英文字符在使用各种编码下, 基本都可以正常解析, 所以一般不带u;但是中文, 必须表明所需编码, 否则一旦编码转换就会出现乱码。 建议所有编码方式采用utf8

字符串前加 “l”

表示宽字符,unicode字符( unicode字符集是两个字节组成的。L告示编译器使用两个字节的 unicode 字符集) 如 L"我的字符串" 表示将ANSI字符串转换成unicode的字符串,就是每个字符占用两个字节。

不加时占用字节
strlen("asd") = 3;
加之后占用字节
strlen(L"asd") = 6;
版权声明
本文为[Python探索牛]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/djdjdj123/p/14318171.html

  1. Experience of learning Python
  2. python7、8章
  3. Chapter 7 and 8 of Python
  4. python bool和str转换
  5. python——循环(for循环、while循环)及练习
  6. python变量和常量命名、注释规范
  7. python自定义异常捕获异常处理异常
  8. python 类型转换与数值操作
  9. python 元组(tuple)和列表(list)区别
  10. 解决python tkinter 与 sleep 延迟问题
  11. python字符串截取操作
  12. Python bool and STR conversion
  13. Python -- loop (for loop, while loop) and Practice
  14. Specification for naming and annotating variables and constants in Python
  15. Python custom exception capture exception handling exception
  16. Python type conversion and numerical operation
  17. The difference between tuple and list in Python
  18. Solve the delay problem of Python Tkinter and sleep
  19. Python string interception operation
  20. Python 100天速成中文教程,GitHub标星7700
  21. Python 100 day quick Chinese course, GitHub standard star 7700
  22. 以我的親身經歷,聊聊學python的流程,同時推薦學python的書
  23. With my own experience, I'd like to talk about the process of learning Python and recommend books for learning python
  24. python爬虫获取起点中文网人气排行Top100(快速入门,新手必备!)
  25. Python crawler to get the starting point of Chinese network popularity ranking Top100 (quick start, novice necessary!)
  26. 【Python常用包】itertools
  27. Itertools
  28. (国内首发)最新python初学者上手练习
  29. (国内首发)最新python初学者上手练习
  30. (first in China) the latest practice for beginners of Python
  31. (first in China) the latest practice for beginners of Python
  32. (数据科学学习手札104)Python+Dash快速web应用开发——回调交互篇(上)
  33. (data science learning notes 104) Python + dash rapid web application development -- callback interaction (Part 1)
  34. (数据科学学习手札104)Python+Dash快速web应用开发——回调交互篇(上)
  35. (data science learning notes 104) Python + dash rapid web application development -- callback interaction (Part 1)
  36. (資料科學學習手札104)Python+Dash快速web應用開發——回撥互動篇(上)
  37. (materials science learning notes 104) Python + dash rapid web application development -- callback interaction (Part 1)
  38. Python OpenCV 图片高斯模糊
  39. Python OpenCV image Gaussian blur
  40. Stargan V2: converse image synthesis for multiple domains reading notes and Python code analysis
  41. 零基础入门Python:基本命令、函数、数据结构
  42. Python: basic commands, functions and data structures
  43. 毫无基础的人如何入门Python?从入门到进阶三份教程,拿走不谢
  44. How can a person without foundation get into Python? From the introduction to the advanced three tutorials, take away
  45. Python设计模式面向对象编程
  46. Python design pattern object oriented programming
  47. Python设计模式面向对象编程
  48. Python design pattern object oriented programming
  49. 怎么样描述你的数据——用python做描述性分析
  50. GitHub上3k+star的python爬虫库你了解吗?详解MechanicalSoup爬虫库
  51. python数据分析——在python中实现线性回归
  52. 疫情来袭,30分钟学会用python开发部署疫情可视化网站
  53. How to describe your data
  54. Do you know the python crawler Library of 3K + star on GitHub? Mechanical soup crawler Library
  55. Python data analysis -- realizing linear regression in Python
  56. When the epidemic strikes, learn to develop and deploy the visualization website of epidemic situation with Python in 30 minutes
  57. 手机上利用python进行数据分析——创建自己的远程jupyter notebook
  58. python数据类型的强制转换
  59. Using Python for data analysis on mobile phones -- creating your own remote jupyter notebook
  60. Mandatory conversion of Python data type