Python基本数据类型之字符串str

字符串的表示方式

  • 单引号 ' '
  • 双引号 " "
  • 多引号 """ """"  、 ''' '''
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

python3的url编码和解码,自定义gbk、utf-8的例子 https://www.jb51.net/article/168181.htm

字符串运算:下标和切片

获取字符串中某个字符

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

# 获取字符串某个字符
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.jb51.net/article/169790.htm

到此这篇关于Python - 基本数据类型_str 字符串的文章就介绍到这了,更多相关Python字符串str内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)

    python3 json数据格式的转换(dumps/loads的使用.dict to str/str to dict.json字符串/字典的相互转换) Python3 JSON 数据解析 JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. Python3 中可以使用 json 模块来对 JSON 数据进行编解码,它包含了两个函数: json.dumps(): 对数据进行编码. json.loads(): 对数据进

  • 详细整理python 字符串(str)与列表(list)以及数组(array)之间的转换方法

    前提: list以及array是python中经常会用到的数据类型,当需要对list以及array进行文件的读写操作的时候,由于write函数参数需要的是一个str,所以这时就需要对list或者array进行str的转换了. list和array的不同: 在进行转换之间先研究下python中list和array(np.array)的不同: 1.list是python中内置的数据类型,其中的数据的类型可以不相同,如java中List也可以不用相同的数据,但是为了格式的统一,就要用到泛型或者Arra

  • python字符串切割:str.split()与re.split()的对比分析

    1.str.split不支持正则及多个切割符号,不感知空格的数量,比如用空格切割,会出现下面情况. >>> s1="aa bb cc" >>> s1.split(' ') ['aa', 'bb', '', 'cc'] 因此split只适合简单的字符分割 2.re.split,支持正则及多个字符切割 >>> print line abc aa;bb,cc | dd(xx).xxx 12.12' xxxx 按空格切 >>&

  • python3格式化字符串 f-string的高级用法(推荐)

    f-string,亦称为格式化字符串常量(formatted string literals),是Python3.6新引入的一种字符串格式化方法,该方法源于PEP 498 – Literal String Interpolation,主要目的是使格式化字符串的操作更加简便. f-string在形式上是以 f 或 F 修饰符引领的字符串(f'xxx' 或 F'xxx'),以大括号 {} 标明被替换的字段:f-string在本质上并不是字符串常量,而是一个在运行时运算求值的表达式: While ot

  • Python格式化字符串f-string概览(小结)

    简介 f-string,亦称为格式化字符串常量(formatted string literals),是Python3.6新引入的一种字符串格式化方法,该方法源于PEP 498 – Literal String Interpolation,主要目的是使格式化字符串的操作更加简便.f-string在形式上是以 f 或 F 修饰符引领的字符串(f'xxx' 或 F'xxx'),以大括号 {} 标明被替换的字段:f-string在本质上并不是字符串常量,而是一个在运行时运算求值的表达式: While

  • python3 字符串/列表/元组(str/list/tuple)相互转换方法及join()函数的使用

    在抓取网络数据的时候,有时会用正则对结构化的数据进行提取,比如 href="https://www.1234.com"等.python的re模块的findall()函数会返回一个所有匹配到的内容的列表,在将数据存入数据库时,列表数据类型是不被允许的,而是需要将其转换为元组形式.下面看下,str/list/tuple三者之间怎么相互转换. class forDatas: def __init__(self): pass def str_list_tuple(self): s = 'abc

  • Python基本数据类型之字符串str

    字符串的表示方式 单引号 ' ' 双引号 " " 多引号 """ """"  . ''' ''' print("hello world") print('hello world') print("""hello world""") # 输出结果 hello world hello world hello world 为什么需要单引号,又需

  • 学好python基本数据类型

    目录 一.基本用法 1.注释 2.输出 3.变量 4.命名规范 5.变量的定义方式 二.python的数据类型 1.字符串类型 2.数字类型 3.List列表类型 4.tuple 元组类型的定义 5.Dict字典类型 6.set集合类型 7.数据类型转换 8.自动类型转换 9.强制类型转换 一.基本用法 1.注释 Python中,#+语句 即为一条注释,也可以用 '''注释块 ''' #人生苦短,我用Python 2.输出 Python中,print()为输出函数 print("Hello Wo

  • python数据类型_字符串常用操作(详解)

    这次主要介绍字符串常用操作方法及例子 1.python字符串 在python中声明一个字符串,通常有三种方法:在它的两边加上单引号.双引号或者三引号,如下: name = 'hello' name1 = "hello bei jing " name2 = '''hello shang hai haha''' python中的字符串一旦声明,是不能进行更改的,如下: #字符串为不可变变量,即不能通过对某一位置重新赋值改变内容 name = 'hello' name[0] = 'k' #通

  • Python字符串str超详细详解(适合新手!)

    目录 1.创建字符串 1.1 使用 ’ ’ 或 " " 创建字符串 1.2 使用 str()函数 转换为字符串 2.访问字符串 2.1 下标索引访问 2.2 切片访问 2.3 for循环遍历字符串 2.4 检查元素是否存在 3.字符串基础知识 3.1 字符串更新 3.2 字符串连接(合并)/复制(重复) 3.3 转义字符 3.4 打印原始字符 r / R 3.5 格式字符串 3.6 三引号 3.7 f-string 3.8 Unicode 字符串 4.内置函数 4.1 打印输出 pri

  • python字符串str和字节数组相互转化方法

    实例如下: # bytes object b = b"example" # str object s = "example" # str to bytes bytes(s, encoding = "utf8") # bytes to str str(b, encoding = "utf-8") # an alternative method # str to bytes str.encode(s) # bytes to str

  • python基础字符串str详解

    目录 字符串str: 编码: ord(字符串)和chr(整数): 字符串字面值: 字符串通用操作 字符串str: 定义:是由一系列字符组成的不可变序列容器,储存的事字符的编码值 编码: 1.字节byte:计算机最小储存单位,等于8位bit 2. 字符:单个的数字,文字与字符 3. 字符集(码表):存储字符与二进制序列的对应关系 4. 编码:将字符转换为对应的二进制序列的过程 5. 解码:将二进制序列转换为对应的字符的过程 6. 编码方式: ASCLL编码:包含英文,数字等字符,每个字符1个字节

  • Python字符串str和json格式相互转换

    目录 1.通过json.loads进行转换 2.json转str 3.通过eval 前言: str转换为json格式,前提一定需要保证这个str的格式和json是一致的,即左边最外层是大括号,右边的最外层是大括号.如果不一致,推荐用正则进行拆分至和json格式一致 1. 通过json.loads进行转换 import json str = '{"name": "御姐", "age": 18}' j = json.loads(str) print(

  • python语言中pandas字符串分割str.split()函数

    目录 前言 1.常规赛数据格式 2.计算詹姆斯常规赛命中率 总结 前言 为了介绍python语言中pandas库在数据分析中的重要作用,本人打算以NBA球星勒布朗詹姆斯在2020-2021赛季常规赛个人数据为例对pandas相关函数进行详细说明.利用爬虫技术,在知名篮球网站虎扑爬取了勒布朗詹姆斯的数据,稍后会将数据上传至csdn,以供大家下载. 这篇文章,详细介绍了pandas字符串分割函数---str.split()的用法. DataFrame.str.split(pa,n,expand)pa

  • Python学习笔记之字符串和字符串方法实例详解

    本文实例讲述了Python学习笔记之字符串和字符串方法.分享给大家供大家参考,具体如下: 字符串 在 python 中,字符串的变量类型显示为 str.你可以使用双引号 " 或单引号 ' 定义字符串 定义字符串 my_string = 'this is a string!' my_string2 = "this is also a string!!!" # Also , we can use backslash '/' to escape quotes. this_strin

  • Python 中的反转字符串reversed(),切片

    目录 一.使用核心 Python 工具反转字符串 二.通过切片反转字符串 三.使用.join()和反转字符串reversed() 四.手动生成反转字符串 五.反转循环中的字符串 六.用递归反转字符串 七.反向遍历字符串 八.该reversed()内置功能 九.切片运算符, [::-1] 十.创建自定义可逆字符串 十一.以相反的顺序对 Python 字符串进行排序 当我们经常在代码中使用 Python 字符串时,您可能需要以相反的顺序使用它们.Python 包含一些方便的工具和技术,可以在这些情况

随机推荐