码迷,mamicode.com
首页 > 其他好文 > 详细

基本概念二-字符串初步

时间:2018-12-03 10:35:22      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:str   引号   pre   python   概念   int   函数   class   col   

这节的开始 需要牢记一条关于Python的规定:

Python在打印所有字符串时,都使用引号将其括起(注意这里说的打印 不是指使用print函数)。这是因为Py在打印值的时候,保留了其在代码中的样子,而不是希望用户看到的样子。

1. python中字符串的转义是 单引号转义为 \‘ 双引号转义为 \"

例如:

1 #我希望可以输出 字符串 Let‘s go "hello world"
2 #应该如此:
3 >>> print(Let\‘s go \"hello world\")

2. 拼接字符串直接使用加号 “+”

例如:

1 >>> "hello," + "world"
2 hello,world
3 #或者
4 >>> x="hello,"
5 >>> y="world"
6 >>> x+y
7 hello,world

3. 关于 str 和 repr

str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式。

例如:

1 #便于解释器解读的形式
2 >>> print(repr("hello,\nworld"))
3 hello,\nworld
4 #便于用户阅读的形式
5 >>> print(str("hello,\nworld"))
6 hello,
7 world

4. 长字符串、原始字符串和字节

 

基本概念二-字符串初步

标签:str   引号   pre   python   概念   int   函数   class   col   

原文地址:https://www.cnblogs.com/kawin/p/10056452.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!