前言

字符串是编程中最重要的数据类型,也是最常见的

字符串的表示方式

  • 单引号 ' '
  • 双引号 " "
  • 多引号 """ """"  、 ''' '''
print("hello world")
print('hello world')
print("""hello world""") # 输出结果
hello world
hello world
hello world

为什么需要单引号,又需要双引号

因为可以在单引号中包含双引号,或者在双引号中包含单引号

# 单双引号
print("hello 'poloyy' world")
print('this is my name "poloyy"') # 输出结果
hello 'poloyy' world
this is my name "poloyy"

多行字符串

正常情况下,单引号和双引号的字符串是不支持直接在符号间换行输入的,如果有需要可以用多引号哦!

# 多行字符串
print("""
hello
world
""")
print("""
this
is
my
name
poloyy
""") # 输出结果
hello
world this
is
my
name
poloyy

转义符

在字符前加 \ 就行

常见的有

  • \n:换行
  • \t:缩进
  • \r:回车

栗子

比如在字符串双引号间还有一个双引号,就需要用转义符

# 转义符
print("hello \"poloyy\" world")
print('my name is \'poloyy\'') # 输出结果
hello "poloyy" world
my name is 'poloyy'

假设 \ 只想当普通字符处理呢?

print("反斜杠 \\ 是什么")
print("换行符是什么 \\n") # 输出结果
反斜杠 \ 是什么
换行符是什么 \n

window 路径的栗子

print("c:\nothing\rtype")
print("c:\\nothing\\rtype") # 输出结果
c:\nothing\
c:
type
c:\nothing\rtype

更简洁的解决方法

用转义符会导致可读性、维护性变差,Python 提供了一个更好的解决方法:在字符串前加 r

print(r"c:\nothing\rtype")

# 输出结果
c:\nothing\rtype

关于更多 r"" 的讲解请看:https://www.cnblogs.com/poloyy/p/12444579.html

字符串运算:下标和切片

获取字符串中某个字符

字符串是一个序列,所以可以通过下标来获取某个字符

# 获取字符串某个字符
str = "hello world"
print(str[0])
print(str[1])
print(str[6])
print(str[-1])
print(str[-5]) # 输出结果
h
e
w
d
l

如果是负数,那么是倒数,比如 -1 就是倒数第一个元素,-5 就是倒数第五个元素

获取字符串中一段字符

Python 中,可以直接通过切片的方式取一段字符

切片的语法格式

str[start : end : step]
  • start:闭区间,包含该下标的字符,第一个字符是 0
  • end:开区间,不包含该下标的字符
  • step:步长

栗子

print("hello world'[:] ", 'hello world'[:])  # 取全部字符
print("hello world'[0:] ", 'hello world'[0:]) # 取全部字符
print("hello world'[6:] ", 'hello world'[6:]) # 取第 7 个字符到最后一个字符
print("hello world'[-5:] ", 'hello world'[-5:]) # 取倒数第 5 个字符到最后一个字符 print("hello world'[0:5] ", 'hello world'[0:5]) # 取第 1 个字符到第 5 个字符
print("hello world'[0:-5] ", 'hello world'[0:-5]) # 取第 1 个字符直到倒数第 6 个字符
print("hello world'[6:10] ", 'hello world'[6:10]) # 取第 7 个字符到第 10 个字符
print("hello world'[6:-1] ", 'hello world'[6:-1]) # 取第 7 个字符到倒数第 2 个字符
print("hello world'[-5:-1] ", 'hello world'[-5:-1]) # 取倒数第 5 个字符到倒数第 2 个字符 print("hello world'[::-1] ", 'hello world'[::-1]) # 倒序取所有字符
print("hello world'[::2] ", 'hello world'[::2]) # 步长=2,每两个字符取一次
print("hello world'[1:7:2] ", 'hello world'[1:7:2]) # 步长=2,取第 2 个字符到第 7 个字符,每两个字符取一次 # 输出结果
hello world'[:] hello world
hello world'[0:] hello world
hello world'[6:] world
hello world'[-5:] world hello world'[0:5] hello
hello world'[0:-5] hello
hello world'[6:10] worl
hello world'[6:-1] worl
hello world'[-5:-1] worl hello world'[::-1] dlrow olleh
hello world'[::2] hlowrd
hello world'[1:7:2] el

字符串的函数

Python 提供了很多内置的字符串函数,具体可看

https://www.cnblogs.com/poloyy/p/12523095.html

 

Python - 基本数据类型_str 字符串的更多相关文章

  1. python基本数据类型之字符串(五)

    python基本数据类型之字符串(五) 遍历与查找 python中的字符串属于可迭代对象,通过一些方法可以遍历字符串中的每一个字符.而查找的方法主要有两个:find与index. 1.字符串的遍历 字 ...

  2. python基本数据类型之字符串(四)

    python基本数据类型之字符串(四) 判断方法 python中有一类用来判断字符串形式的方法,该类方法有两个特点:(1)方法名都是is开头(除了startswith和endswith):(2)返回值 ...

  3. python基本数据类型之字符串(三)

    python基本数据类型之字符串(三) 转换和判断方法 在python中,有一些内置方法可以将字符串转化特定形式,而与之对应的一些方法可以判断字符串是否符合某些形式.因此,在这篇文章中,笔者把转换方法 ...

  4. python基本数据类型之字符串(二)

    python基本数据类型之字符串(二) 替换方法 python中字符串的替换方法主要有:center.rjust\ljust.expandtabs.format\format_map(格式化).str ...

  5. Python基础数据类型之字符串

    Python基础数据类型之字符串 一.Python如何创建字符串 在python中用引号将一些文本包起来就构成了字符串(引号可以是单引号.双引号.单三引号,双三引号,它们是完全相同的) >> ...

  6. python自学笔记(二)python基本数据类型之字符串处理

    一.数据类型的组成分3部分:身份.类型.值 身份:id方法来看它的唯一标识符,内存地址靠这个查看 类型:type方法查看 值:数据项 二.常用基本数据类型 int 整型 boolean 布尔型 str ...

  7. Python基本数据类型之字符串、数字、布尔

     一.数据类型种类 Python中基本数据类型主要有以下几类: Number(数字) String(字符串) Bool (布尔) List(列表) Tuple(元组) Sets(集合) Diction ...

  8. Python基础 数据类型 (字符串、列表、字典、元组、集合、堆、栈、树)

    数据类型有整型.布尔.字符串.列表.字典.元组.集合.堆.栈和树. 1.整型: 整型就是数字 数字表示 python2 64位机器,范围-2^63~2^63-1 超出上述范围,python自动转化为l ...

  9. Python开发——数据类型【字符串】

    字符串定义 字符串是一个有序的字符的集合,用于存储和表示基本的文本信息 在Python中加了引号的字符,都被认为是字符串! 单引号.双引号.多引号之间的区别? 答案:单双引号没有区别 多引号的作用? ...

随机推荐

  1. [论文阅读笔记] LouvainNE Hierarchical Louvain Method for High Quality and Scalable Network Embedding

    [论文阅读笔记] LouvainNE: Hierarchical Louvain Method for High Quality and Scalable Network Embedding 本文结构 ...

  2. 【Azure 应用服务】由 Azure Functions runtime is unreachable 的错误消息推导出 ASYNC(异步)和 SYNC(同步)混用而引起ThreadPool耗尽问题

    问题描述 在Azure Function Portal上显示: Azure Functions runtime is unreachable,引起的结果是Function App目前不工作,但是此前一 ...

  3. Oracle数据库使用pfile启动还是spfile启动---oracle

    查看数据库使用pfile启动还是spfile启动 9i版本以后,一般是使用spfile启动,但前提是有这个spfile文件,如果同时存在spfile和pfile文件,会优先选择spfile模式启动数据 ...

  4. 所有处理都走向AI

    所有处理都走向AI All Processing Bends Toward AI 旧金山--谷歌正在试验机器学习(ML)来执行集成电路设计中的位置和路径,并取得了很好的效果.上周在ISSCC会议上宣布 ...

  5. GD32F330 | ADC实例 基于DMA方式

    GD32F330 | ADC实例 基于DMA方式 简单记录一下 ADC多通道转换 DMA搬运 的使用,以 GD32F330G8U6 为例: 一.ADC 基础知识 12位ADC是一种采用逐次逼近方式的模 ...

  6. Redis不是只有get set那么简单

    我以前还没接触Redis的时候,听到大数据组的小伙伴在讨论Redis,觉得这东西好高端,要是哪天我们组也可以使用下Redis就好了,好长一段时间后,我们项目中终于引入了Redis这个技术,我用了几下, ...

  7. kube-controller-manager源码分析-AD controller分析

    kubernetes ceph-csi分析目录导航 概述 kube-controller-manager组件中,有两个controller与存储相关,分别是PV controller与AD contr ...

  8. 尝鲜一试,Azure静态网站应用服务(Azure Static Web Apps) 免费预览,协同Github自动发布静态SPA

    背景 最近在浏览微软的文档的时候发现,微软喜欢用Hugo这个文档框架,有些技术产品的文档页面就用Hugo来做的,同时搭配Github + Azure Static Web Apps Service这个 ...

  9. 重新整理 .net core 实践篇————网关[三十六]

    前言 简单整理一下网关. 正文 在介绍网关之前,介绍一下BFF,BFF全称是Backend For Frontend,它负责认证授权,服务聚合,目标是为前端提供服务. 说的通透一点,就是有没有见过这种 ...

  10. 并发王者课-铂金8:峡谷幽会-看CyclicBarrier如何跨越重峦叠嶂

    欢迎来到<并发王者课>,本文是该系列文章中的第21篇,铂金中的第8篇. 在上一篇文章中,我们介绍了CountDownLatch的用法.在协调多线程的开始和结束时,CountDownLatc ...