python数据类型string_Python的基本数据类型———String

String是一个Unicode字符序列,是Python中最重要的数据类型之一,可以使用单引号、双引号、三引号创建

创建一个字符串

1

2

3a='1234'

b="hello world"

c='''hello world 1234'''

既然单引号、双引号、三引号都可以创建字符串,那么他们的区别在哪呢?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18a='hello world'

b="what's up ?"

# 如果我们输出一篇文章或者换行的时候可以试试三引号

c='''

hello world

hello world

'''

# 输出

hello world

hello world

字符串中的转义字符

如何使用python打印这样一段字符串’D:Download/note/notebook’,使用print()直接能打印出来吗?我们来演示一下

1

2

3

4

5

6

7a='D:Downloadnotenotebook'

print(a)

# 输出结果

D:Download

ote

otebook

结果不仅少了两个n而且还换行了,这是因为n是转义字符,会被当成换行符。

转义字符在字符串中使用的很广泛,下面我们通过代码了解一下转义字符以及用法

1

2

3

4

5

6

7

8

9

10

11

12# 我们使用两种方式打印完整的路径

a='D:Downloadnotenotebook'

# 通过在n前面添加

print('D:Download\note\notebook')

# 在字符串前添加r或R

print(r'D:Downloadnotenotebook')

# 让hello world换行

a='hello world'

print('hello nworld')

python中支持的转义字符有以下几种

转义字符

python中输出

‘(单引号)

“(双引号)

a

响铃(输出有声音)

b

退后一格

f

换页(打印)

n

换行

r

回车

t

横向制表符

v

纵向制表符

怎么访问字符串中的字符

可以通过切片、索引访问字符串中的字符,和大多数语言一样,索引是从0开始并且必须是整型

我们先来看看索引的使用方式

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26a='Hello world,你好 世界'

# 获取第一个字符

print(a[0])

# 输出 H

# 获取w

print(a[6])

# 输出 w

# 在python中空格也算是一个字符,所以是索引是6而不是5

# 获取最后一个字符串界

print(a[16])

# 这段字符串比较短,你可以数出来,但是如果是一篇文章呢

# 所以python中还提供了一种简单的方式:负数

print(a[-1]) # 界

print(a[-2]) # 世

# 注意:通过索引访问时,如果索引不存在将会报错,如下

print(a[50])

Traceback (most recent call last):

File "", line 1, in

IndexError: string index out of range

通过上面索引的方式我们可以获取字符串中的某个字符,但是如果我们想获取多个字符怎么解决呢?这个时候就要使用切片了

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18a='Hello world,你好 世界'

# 获取'Hello world'

print(a[0:11])

# 获取'你好 世界'

print(a[12:17])

# 通过上面的索引,我们可以尝试使用切片用负数获取最后一个字符

# 获取 '你好 世界'

print(a[-5:])

# 和上面类型的写法还有以下几种

print(a[:-6])

# 输出 Hello world

print(a[5:])

# 输出 world,你好 世界

思考一下:通过不存在的索引访问字符串会报错,在切片中范围不存在,会怎么样呢?这个留给大家有空测试一下

小结:通过上面几个案例,我们可以总结以下结论

字符串中的索引从0开始

索引和切片中都可以使用负数,表示倒数第几个

切片中冒号前后的数字可以省略一个,分别表示从头到…或者从…到结束

字符串的一些基本操作字符串的合并

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22a='hello'

b='world'

# 合并两个字符串

print(a+b)

helloworld

# helloworld中间加空格

print(a+' '+b)

# 输出10个hello

print(a+a+a+a+a+a+...)

# 这里我们可以使用乘法,就不用加10次了

print(a*10)

hellohellohellohellohellohellohellohellohellohello

# 上面的结果可能不是我们想要

# 那问题来了

# 这10个hello里面如何添加空格或逗号呢

print((a+' ')*10)

hello hello hello hello hello hello hello hello hello hello

小思考 通过上面的案例我们知道字符串可以相加、可以和数字相乘

那字符串和字符串能相乘吗?字符串和0相乘又是什么呢?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25a='Hello'

b='World'

print(a*b)

print(a*0)

.

.

.

a*b

# 会报错,告诉我们字符串不能和字符串相乘

TypeError: can't multiply sequence by non-int of type 'str'

a*0

# 输出 空

# 可以通过下面两个函数判断具体输出内容

# 判断字符串的长度

print(len(a*0))

0

# 判断字符串的类型

print(type(a*0))

str

字符串的一些常用方法in 、not in判断字符串中是否包含指定的字符

1

2

3

4

5

6

7

8

9

10# 判断 hello 是否存在于hello world

a='hello'

b='hello world'

print(a in b)

# 输出 True

# 判断 hello 是否不存在于 hello world

print(a not in b)

# 输出 False

.capitalize() 首字母转为大写其他英文字母转为小写

1

2

3a='hELLO WORLD 12345'

print(a.capitalize())

# 输出 Hello world 12345

.swapcase() 大写转为小写,小写转为大写

1

2

3a='hELLO world'

print(a.swapcase())

# 输出 Hello WORLD

.lower() 全部转为小写

1

2

3a='hELLO WORLD 12345'

print(a.lower())

# 输出 hello world 12345

.upper() 全部转为大写

1

2

3a='hello World'

print(a.upper())

# 输出 HELLO WORLD

.title() 每个单词的首字母转为大写

1

2

3a='this is an apple'

print(a.title())

# 输出 This Is An Apple

.count() 统计字符串中字符出现的次数

1

2

3

4

5

6

7

8

9

10

11# .count([, [, ]])

a='Hello worLd'

# 'l'在字符串中出现的次数

print(a.count('l'))

# 输出 2 大写的不会被统计

# 'o'在字符串0-5之前出现的次数

print(a.count('o',0,5))

# 输出 1

.endswith() 判断字符串是否以某字符结尾

1

2

3

4

5

6

7

8

9

10# .endswith([, [, ]])

a='Hello world'

# 判断是否以'world'结尾

print(a.endswith('world'))

# 输出 True

# 判断'Hello' 是否以'w'结尾

print(a.endswith('w',0,5))

.startswith() 判断字符串是否以某字符开头

1

2

3

4

5

6

7

8

9

10

11# .startswith ([, [, ]])

a='Hello world'

# 判断是否以'world'开头

print(a.startswith('world'))

# 输出 False

# 判断'Hello' 是否以'H'开头

print(a.startswith('H',0,5))

# 输出 True

.find() 查找字符串的位置

1

2

3

4

5

6

7

8

9

10

11

12# find([, [, ]])

a='Hello world'

# 查找'world'在字符串中的位置

print(a.find('world'))

# 输出 6

# 如果查找的对象不存在则返回-1

print(a.find('Worlds'))

# .index()的使用方法和功能类似,但是.index() 查找不到会报错

.center() 给字符串前后添加空格或字符并让给定字符串居中

1

2

3

4

5

6

7

8

9

10a='Hello world'

print(a.center(20))

# 输出 ' Hello world '

print(a.center(20,'='))

# 输出 ====Hello world=====

# 注意给定的长度必须大于字符串的长度

print(a.center(5,'='))

# 这样就没有效果

.replace() 替换给定字符

1

2

3

4

5# .replace(, [, ])

a='Hello world'

print(a.replace('world','python'))

# 输出 Hello python

1

2

3

4

5

6

7

8

9a=' Hello world '

# 删除前后空格

print(a.strip())

# 输出 Hello world

# 移除给定字符

print(a.strip('Hd'))

# 输出 ello worl

关于字符串转化list和string的相互转化,会在后面分享

好了,本篇字符串的分享到这里就结束了,关于字符串的方法还有很多,不一定要全部记住背会,先大致了解有个印象,在日后使用的时候再去查找,用的多了自然就记住啦

总结

本篇分享知识点很多,重点掌握字符串的索引和切片使用方法,关于字符串中的转义字符多多练习应该是没什么问题,python为字符串提供了大量的方法,先大致有个了解,可以方便日后查询

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值