python (个人学习笔记1.6)-----有关字符串的多种操作与方法(函数)调用

一、字符串(string)

1.什么是字符串(string)(简单描述)

字符串:字符串(String)是由数字、字母、下划线组成的一串字符。它是编程语言中表示文本的数据类型。

例如:

str1 = 'This si a string,111111'

2.字符串(string)相关操作

(1).字符串(string)重复打印符:*

*(星号):可以使同一字符串重复打印 * n倍

例如:

str1 = 'This is a string,111111\n'
print(str1 * 6)

输出:

This is a string,111111
This is a string,111111
This is a string,111111
This is a string,111111
This is a string,111111
This is a string,111111

(2).切片获取字符(操作与列表相仿)

操作格式:str1[ 开始位置 :结束位置 :步长 ]

例如:

str1 = 'This is a string,111111\n'
str2 = str1[1:5:2]
print(str1[:7:1])
print(str2)

输出:

This is
hs

(3).关键字 in

in :可判断一段字符是否存在某一字符串中,存在则返回True,反之返回False

例如:

print(123 in [23,45,123])
print('llo' in 'hello')
print('e2l' in 'hello')

输出:

True
True
False

(4).字符串合并字符串,或字符串后追加字符串的两种操作

①.join() 方法(函数)

join(): 使用指定字符连接多个字符串
格式:<变量> = ’ 选择用作连接的字符 ’ . join( 选择连接的多个字符串)

例如:

a='123'
b='abc'
d='44'

c= ''.join([a,b,d])			
e= '----'.join([a,b,d])
a= ''.join([a,b,d])			#改变原字符串a的内容

print(c)
print(e)
print(a)

输出:

123abc44
123----abc----44
123abc44
②.直接使用’+'连接字符串(string)。(不推荐使用)

例如:

a='123'
b='abc'
d='44'

f = a+b+d+'ccccc'
c = a+b+d
print(f)
print(c)

输出:

123abc44ccccc
123abc44

3.字符串(string)常用的几种内置方法

(1).count() 方法(函数)

count(): 查询某个字符在字符串中出现的次数

例如:

str1 = 'This is a string,111111\n'

print(str1.count('s'))
print(str1.count('1'))

输出:

3
6

(2).center() 方法(函数)

center() : 用某一字符使目标字符串居中
格式:<目标变量> . center( 包括目标变量在内的长度 ,填充的字符 )

例如:

str1 = 'This is a string,111111'

print(str1.center(50,"#"))  # 该50长度包含str1字符串长度,即除开str1字符串,其他空位用#号填充至长度为50
print(str1.center(60,'$'))

输出:

#############This is a string,111111##############
$$$$$$$$$$$$$$$$$$This is a string,111111$$$$$$$$$$$$$$$$$$$

(3).startswith() 方法(函数)

startswith() :判断字符串是否以某一段字符作为开头,是返回True,反之返回False

例如:

str1 = 'This is a string,111111'

print(str1.startswith("Th"))
print(str1.startswith("This is"))
print(str1.startswith("str"))

输出:

True
True
False

(4).find() 方法(函数)

find() : 查找某一字符在字符串中第一次出现的位置(与列表中的find()方法相似)

例如:

str1 = 'This is a string,111111'

print(str1.find("i"))
print(str1.find('1'))

输出:

2
17

(5).format() 方法(函数)

format() : 字符串中,一种特殊的格式化输出方式

例如:

str1 = 'This is {name},he is {age} years old'

print(str1.format(name='ligang',age=30))

输出:

This is ligang,he is 30 years old

(6).lower() 与 upper() 方法(函数)

①.lower() 方法(函数)

lower(): 将字符串中,所有字符串的字母转换为小写

例如:

str1 = 'This is a sTrinG,Hahahahaha 111111'
print(str1.lower())

输出:

this is a string,hahahahaha 111111
②.upper() 方法(函数)

upper(): 将字符串中,所有字符串的字符转换为大写

例如:

str1 = 'This is a sTrinG,Hahahahaha 111111'
print(str1.upper())

输出:

THIS IS A STRING,HAHAHAHAHA 111111

(7).strip() 方法(函数)

strip() : 删除字符串开头和末尾的所有特殊字符,例如换行符\n,缩进\t

例如:

str1 = '\t\tThis is a sTrinG,Hahahahaha 111111\n\n'

print(str1.strip())
print('zheshi')

输出:

This is a sTrinG,Hahahahaha 111111		
zheshi

(8).replace() 方法(函数)

replace() : 将字符串中的某一字符串替换为另一字符串,可设置替换次数

例如:

str1 = 'Th is is a sTrinG,Ha is haha is ha is ha'
print(str1.replace('is','666',1))
print(str1.replace('is','888',3))

输出:

Th 666 is a sTrinG,Ha is haha is ha is ha		#从左往右一个‘is’被替换成‘666’
Th 888 888 a sTrinG,Ha 888 haha is ha is ha		#从左往右三个‘is’被替换成‘888’

(9).split() 方法(函数)

split() :将字符串返回成列表,并将字符串内的某一指定字符转换为分隔符(,),可以指定转换的个数。如果字符串内没有指定字符,那么将使所有字符串转换成列表内部的一个元素。

例如:

str1 = '$ is is $ $,$ is $ is $ is $'
print(str1.split(' '))
print(str1.split('is',3))
print(str1.split('l',4))
print(str1)

输出:

['$', 'is', 'is', '$', '$,$', 'is', '$', 'is', '$', 'is', '$']	#没有指定替换个数,默认将字符串中所有的空格转换成分隔符(,)逗号,转换成列表并返回列表
['$ ', ' ', ' $ $,$ ', ' $ is $ is $']	#替换三个is
['$ is is $ $,$ is $ is $ is $']	#字符串中没有‘l’字符,字符串不分隔
$ is is $ $,$ is $ is $ is $		#原变量还是字符串,没有被改变为列表
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值