Python - 基础数据类型 str 字符串

简介: 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

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

 

字符串运算:+ 运算


直接拼接的作用

# + 运算
print("123" + "123")
print("123" + "abc")
print("123", "456")
# 输出结果
123123
123abc
123 456


字符串运算:下标和切片


获取字符串中某个字符

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

# 获取字符串某个字符
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, end) 范围的子字符串
  • start:闭区间,包含该下标的字符,第一个字符是 0
  • end:开区间,不包含该下标的字符
  • step:步长,设为 n,则每隔 n 个元素获取一次

 

栗子

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


获取字符串长度


print(len("123"))


# 输出结果

3

 

字符串的函数


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

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

相关文章
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
691 100
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
792 99
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
开发者 Python
Python f-strings:更优雅的字符串格式化技巧
Python f-strings:更优雅的字符串格式化技巧
|
存储 Java 索引
(Python基础)新时代语言!一起学习Python吧!(二):字符编码由来;Python字符串、字符串格式化;list集合和tuple元组区别
字符编码 我们要清楚,计算机最开始的表达都是由二进制而来 我们要想通过二进制来表示我们熟知的字符看看以下的变化 例如: 1 的二进制编码为 0000 0001 我们通过A这个字符,让其在计算机内部存储(现如今,A 字符在地址通常表示为65) 现在拿A举例: 在计算机内部 A字符,它本身表示为 65这个数,在计算机底层会转为二进制码 也意味着A字符在底层表示为 1000001 通过这样的字符表示进行转换,逐步发展为拥有127个字符的编码存储到计算机中,这个编码表也被称为ASCII编码。 但随时代变迁,ASCII编码逐渐暴露短板,全球有上百种语言,光是ASCII编码并不能够满足需求
542 4
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1997 102
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
710 104
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
566 103
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
619 82
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
730 3

推荐镜像

更多