Python基础语法(二)——字符串

目录

1.基本形式

(1)单引号、双引号: 

 (2)三引号:

 2.下标(索引)

3.切片

4.常用操作方法

(1)find()

(2)index()

(3)count()

(4)replace()

(5)split()

(6)join()

 (7)capitalize() &&  title()&&  lower()&&  upper()

(8)startswith()&&  endswith()

(9)isalpha()&&  isdigit()&&  isalnum()&&  isspace() 

Python基础语法学习笔记,原文来自: 

深入浅出Python——Python基础语法全解_何极光的博客-CSDN博客_深入浅出python

1.基本形式

(1)单引号、双引号: 

a = 'hello world'
b = "abcdefg"
print(type(a))
print(type(b))

注意:控制台显示结果为<class 'str'>, 即数据类型为str(字符串)。单引号和双引号均属于str类型(字符串)

 (2)三引号:

name3 = ''' Tom '''
name4 = """ Rose """
a = ''' i am Tom, 
        nice to meet you! '''

b = """ i am Rose, 
        nice to meet you! """

注意:三引号形式的字符串支持换行。

 2.下标(索引)

name = "abcdef"

print(name[1])
print(name[0])
print(name[2])

注意:下标从0开始

输出:

b

a

c

3.切片

name = "abcdefg"

print(name[2:5:1])  # cde
print(name[2:5])  # cde
print(name[:5])  # abcde
print(name[1:])  # bcdefg
print(name[:])  # abcdefg
print(name[::2])  # aceg
print(name[:-1])  # abcdef, 负1表示倒数第一个数据
print(name[-4:-1])  # def
print(name[::-1])  # gfedcba

注意:

  • 不包含结束位置下标对应的数据, 正负整数均可;
    步长是选取间隔,正负整数均可,默认步长为1。

4.常用操作方法

(1)find()

mystr = "hello world and buran and list and Python"

print(mystr.find('and'))  # 12
print(mystr.find('and', 15, 30))  # 22
print(mystr.find('ands'))  # -1
字符串序列.find(子串, 开始位置下标, 结束位置下标)

(2)index()

mystr = "hello world and buran and list and Python"

print(mystr.index('and'))  # 12
print(mystr.index('and', 15, 30))  # 22
print(mystr.index('ands'))  # 报错

注意:这里与find不同的是,当查找不到元素的是直接报错,而不是返回-1. 

  • rfind(): 和find()功能相同,但查找方向为右侧开始。
  • rindex():和index()功能相同,但查找方向为右侧开始。

(3)count()

返回某个子串在字符串中出现的次数。

mystr = "hello world and buran and list and Python"

print(mystr.count('and'))  # 3
print(mystr.count('ands'))  # 0
print(mystr.count('and', 0, 20))  # 1

(4)replace()

mystr = "hello world and buran and list and Python"

print(mystr.replace('and', 'he'))
# 结果:hello world he buran he list he Python
print(mystr.replace('and', 'he', 10))
# 结果:hello world he buran he list he Python
print(mystr)
# 结果:hello world and buran and list and Python

 注意:数据按照是否能直接修改分为可变类型和不可变类型两种。字符串类型的数据修改的时候不能改变原有字符串,属于不能直接修改数据的类型即是不可变类型。

(5)split()

mystr = "hello world and buran and list and Python"

print(mystr.split('and'))
# 结果:['hello world ', ' buran ', ' list ', ' Python']
print(mystr.split('and', 2))
# 结果:['hello world ', ' buran ', ' list and Python']
print(mystr.split(' '))
# 结果:['hello', 'world', 'and', 'buran', 'and', 'list', 'and', 'Python']
print(mystr.split(' ', 2))
# 结果:['hello', 'world', 'and buran and list and Python']

 注意:num表示的是分割字符出现的次数,即将来返回数据个数为num+1个。

(6)join()

list1 = ['hello', 'buran', 'list']
t1 = ('aa', 'b', 'cc', 'ddd')

print('_'.join(list1))
# hello_buran_list
print('...'.join(t1))
# 结果:aa...b...cc...ddd

 (7)capitalize() &&  title()&&  lower()&&  upper()

将字符串第一个字符转换成大写

mystr = "hello world and buran and list and Python"

print(mystr.capitalize())
# 结果:Hello world and buran and list and python

 将字符串每个单词首字母转换成大写

mystr = "hello world and buran and list and Python"

print(mystr.title())
# 结果:Hello World And Buran And List And Python

大写转成小写

mystr = "hello world and buran and list and Python"

print(mystr.lower())
# 结果:hello world and buran and list and python

小写转成大写

mystr = "hello world and buran and list and Python"

print(mystr.upper())
# 结果:HELLO WORLD AND BURAN AND LIST AND PYTHON
  • lstrip():删除字符串左侧空白字符。
  • rstrip():删除字符串右侧空白字符。
  • strip():删除字符串两侧空白字符。

(8)startswith()&&  endswith()

mystr = "hello world and buran and list and Python"

print(mystr.startswith('hello'))
# 结果:True
print(mystr.startswith('hello', 5, 20))
# 结果:False
mystr = "hello world and buran and list and Python"

print(mystr.endswith('Python'))
# 结果:True
print(mystr.endswith('python'))
# 结果:False
print(mystr.endswith('Python', 2, 20))
# 结果:False

(9)isalpha()&&  isdigit()&&  isalnum()&&  isspace() 

如果字符串至少有一个字符并且所有字符都是字母则返回 True, 否则返回 False。

mystr1 = 'hello'
mystr2 = 'hello12345'

print(mystr1.isalpha())
# 结果:True
print(mystr2.isalpha())
# 结果:False

如果字符串只包含数字则返回 True 否则返回 False

mystr1 = 'aaa12345'
mystr2 = '12345'

print(mystr1.isdigit())
# 结果: False
print(mystr2.isdigit())
# 结果:True

如果字符串至少有一个字符并且所有字符都是字母或数字则返 回 True,否则返回 False。

mystr1 = 'aaa12345'
mystr2 = '12345-'

print(mystr1.isalnum())
# 结果:True
print(mystr2.isalnum())
# 结果:False

如果字符串中只包含空白,则返回 True,否则返回 False。

mystr1 = '1 2 3 4 5'
mystr2 = '     '

print(mystr1.isspace())
# 结果:False
print(mystr2.isspace())
# 结果:True

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值