IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> Python字符串的运算及转义字符 -> 正文阅读

[Python知识库]Python字符串的运算及转义字符

字符串所用字符(’ ’ or " ")

>>> 'hello world'
'hello world'
>>> "hello world"
'hello world'

为什么既有单引号又有双引号

打印出let’s go

>>> "let's go"
"let's go"
>>> 'let\'s go'
"let's go"

终端打印多行

>>> '''
... hello world
... hello kaola
... hello python
... '''
'\nhello world\nhello kaola\nhello python\n'
>>> """
... hello world
... hello kaola
... hello python
... """
'\nhello world\nhello kaola\nhello python\n'

字符串转义字符换行\n

>>> print(''' hello wolrd\n hello kaola\n hello python\n''')
 hello wolrd
 hello kaola
 hello python

’ 单引号
\t 横向制表符(相当于tab)
\n 换行
\r 回车

打印出’hello \n world’

>>> print('hello \\n world')
hello \n world

使用场景一:打印出磁盘目录 c:\northwide\northwest
除了加转义符号外,还可以在字符串前加r或者R

>>> print('c:\northwide\northwest')
c:
orthwide
orthwest
>>> print('c:\\northwide\\northwest')
c:\northwide\northwest
>>> print(r'c:\northwide\northwest')
c:\northwide\northwest

合并字符串

  1. ‘+’ (拼接)
>>> 'hello'+'world'
'helloworld'
  1. ‘*’(重复)
>>> 'hello'*3
'hellohellohello'
  1. ‘[n]’(取对应索引下字符)
>>> 'hello world'[3]
'l'
>>> 'hello world'[0]
'h'
  1. ‘[-n]’(从后往前数)
>>> 'hello world'[-5]
'w'
  1. ‘[m:n]’(截取一串字符)
>>> 'hello world'[0:4]
'hell'
>>> 'hello world'[0:-1]
'hello worl'
  1. ‘[m:]’(从索引m截取到字符串的末尾)
>>> 'hello world'[6:]
'world'

7.‘[-m:]’ 如果字符串很长的情况下从末尾开始截取

>>> 'hello java python c# ruby'[-4:]
'ruby'
  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-07-03 10:44:49  更:2022-07-03 10:46:07 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/4 5:36:12-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码