2020.9.11字符串的笔记

Str()实现数字转型字符串

使用 Str()将其他数据类型转换成字符串

字符串的本质就是字符序列
正向搜素:最左侧第一个字符偏移量是0,第二个偏移量是1,第三个偏移量是2,第四个偏移量是3,以此类推,知道len(str)-1为止
反向搜素:最左侧第一个字符偏移量是0,第二个偏移量是-1,第三个偏移量是-2,第四个偏移量是-3,以此类推,知道-len(str)为止
字符串不可改变,我们有时候确实需要替换某些字符串,这时只能通过创建新的字符串来实现

下面是Demo

>>> a='abdssdghskhsdjhfdksf'
>>> a
'abdssdghskhsdjhfdksf'
>>> a[0]
'a'
>>> a[3]
's'
>>> a[26-1]
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    a[26-1]
IndexError: string index out of range
>>>  a[16-1]
 
SyntaxError: unexpected indent
>>> a[15]
'f'
>>> a[-1]
'f'
>>> a[-3]
'k'
>>> a.replace('c','高')
'abdssdghskhsdjhfdksf'
>>> c
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    c
NameError: name 'c' is not defined
>>> a
'abdssdghskhsdjhfdksf'
>>> a=a.replace('c','高')
>>> a
'abdssdghskhsdjhfdksf'
>>> 'abdssdghskhsdjhfdksf'
'abdssdghskhsdjhfdksf'
>>> a='abdssdghcskhsdjhfdksf'
>>> a=a.replace('c','高')
>>> a
'abdssdgh高skhsdjhfdksf'
>>> a[16-1]
'h'
>>>  
使用字符串切片slice操作可以让我们快速的提取字符串

起始偏移量start:终止偏移量end:步长step

下面是Demo

>>> a = 'asfsbdfjbsdf'
>>> a[2]
'f'
>>> a[1:5]
'sfsb'
>>> a[1:5:2]
'ss'
>>> a[:]
'asfsbdfjbsdf'
>>> a[2:]
'fsbdfjbsdf'
>>> a[:3]
'asf'
>>> a[-2]
'd'
>>> a[-8:-3]
'bdfjb'
>>> a[::-1]
'fdsbjfdbsfsa'
>>> a[2:400]
'fsbdfjbsdf'
>>> 'to be or not to be'[::-1]
'eb ot ton ro eb ot'
>>> 'sxtsxtsxtsxt'[::3]
'ssss'
>>> 

####字符串分割split()和join()合并

split()可以基于指定分隔符将字符串分割成多个子字符串(存储到列表中)
join()的作用和split()刚好相反,用于将一系列字符串连接起来

下面是Demo

>>> a = "to be or not to be"
>>> a.split()
['to', 'be', 'or', 'not', 'to', 'be']
>>> a.split('to')
['', ' be or not ', ' be']
>>> 'abc'+'def'
'abcdef'
>>> a=['sxt','sxt100','sxt200']
>>> '*'.join(a)
'sxt*sxt100*sxt200'
>>> "".join(a)
'sxtsxt100sxt200'
>>> 
练习

通过练习来巩固之前学的知识
下面是Demo

import time

time01 = time.time() #起始时间


a = ''
for i in range(10000000):
    a += 'sxt'

time02 = time.time() #终止时间


print('运算时间:'+str(time02-time01))

li = []
time03 = time.time() #起始时间
for i in range(1000000):
    li.append('sxt')


a = ''.join(li)
time04 = time.time() #终止时间
print('运算时间:'+str(time04-time03))

字符串的驻留机制和字符串比较

字符串驻留:仅保存一份相同且不可变字符串的方法,不同的值被存放在字符串驻留池中
python支持字符串驻留机制,对于符合标识符规则的字符串(仅包含下划线(_))丶字母和数字)会启用字符串驻留机制
使用==,!=对字符串进行比较,是否含有相同的字符
使用is/not is,判断两个对象是否同一个对象,比较的对象是地址,即ID(obj)是否和id(obj2)相等
成员操作符:in/not in 关键字,判断某个字符(子字符串)是否存在于字符串中
下面是Demo

>>> a = 'abcdefg'
>>> 'b' in a
True
>>> 'bcd' in a
True
>>> 'uji' in a
False
>>> 
字符串的常用方法汇总

常用的查找方法:len(),字符串长度,a.startswinth(‘我是高崎’),指定字符串开头,a.endswith(‘过我’),指定字符串结尾,a.find(‘高’),第一次出现指定字符串的位置,a.rfind(‘高’),a.count(‘编程’),指定字符串出现几次,a.isalnum()所有字符全是字母和数字
去除首尾信息:我们通过strip()去除字符串首尾指定的信息,通过istrip()去除两边
大小写的转换:编程中关于字符串大小写转换的情况,经常遇到,我们将其方法汇总到这:a.capitalize()产生新的字符串,首字符大写,a.title()产生新的字符串,每个单词首字母都是大写,a.upper()产生新的字符串,所有字符全转成大写,a.lower(),产生新的字符串,所有字符转成小写,a.swapcase(),产生新的,所有字母大小写转换
格式排版:center(),ljust(),rjust()这个三个函数对字符串实现排版
其他方法:1.isalnum(),是否为字母或者数字 2.isalpha(),检测字符串是否只由字母组成(含汉字)3.isdigit(),检测字符串是否由数字组成 4. isspace,检测是否为空白符 5. isupper()是否为大写字母 6. islower 是否为小写字母

>>> 'sxt'.strip
<built-in method strip of str object at 0x00000139C82F4D70>
>>> ' sxt '.strip
<built-in method strip of str object at 0x00000139C82F4DB0>
>>> 'sxt'.strip()
'sxt'
>>> ' s x t '.strip()
's x t'
>>> '*s*x*t'.strip()
'*s*x*t'
>>> '*s*x*t*'.strip()
'*s*x*t*'
>>> '*s*x*t'.strip('*')
's*x*t'
>>> '*s*x*t*'.strip('*)
		
SyntaxError: EOL while scanning string literal
>>> '*s*x*t*'.strip('*)
		
SyntaxError: EOL while scanning string literal
>>> '*s*x*t*'.strip('*')
's*x*t'
>>> 
#格式转换
>>>> a = 'gaoqi,love progrmming,love SXT'
>>> a.capitalize()
'Gaoqi,love progrmming,love sxt'
>>> a.title(a)
Traceback (most recent call last):
  File "<pyshell#46>", line 1, in <module>
    a.title(a)
TypeError: title() takes no arguments (1 given)
>>> a.title(a)
	
SyntaxError: invalid character in identifier
>>> a.title(a)
Traceback (most recent call last):
  File "<pyshell#48>", line 1, in <module>
    a.title(a)
TypeError: title() takes no arguments (1 given)
>>> a.title()
'Gaoqi,Love Progrmming,Love Sxt'
>>> a.upper()
'GAOQI,LOVE PROGRMMING,LOVE SXT'
>>> a.lower()
'gaoqi,love progrmming,love sxt'
>>> a.swapcase()
'GAOQI,LOVE PROGRMMING,LOVE sxt'
>>> 
#格式排版
>>> n = 'sxt'
>>> a.center(10,'*')
'gaoqi,love progrmming,love SXT'
>>> n.center(10,'*')
'***sxt****'
>>> n.ljust(20,'@')
'sxt@@@@@@@@@@@@@@@@@'
>>> n.rjust(30,'#')
'###########################sxt'
>>> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值