python简要整理03_字符串基础及常用操作

Python简要整理02


十二、字符串

(1)基础

定义

Name1=‘Hello World'
Name2='''Hello World
		 Hello World
		 Hello World
	  '''
Name3='Hello'\
	  'Hello'\
      'Hello'
#空字符串
Name4=''
#输入
input()

下标和切片

  1. 下标
#从左至右
Name='abcde'
for i in range(5):
    print(Name[i],'是第%d位' %i)
>>>
a 是第0位
b 是第1位
c 是第2位
d 是第3位
e 是第4位

#从右至左
index = -1
while index >= -5:
    print(name[index],'是第%d位' %index)
    index -=1
>>>
e 是第-1位
d 是第-2位
c 是第-3位
b 是第-4位
a 是第-5位
  1. 切片
# 切片指截取某一对象的其中一部分的操作
# (字符串\列表\元组均支持切片操作)

#语法:[起始:结束:步长]

name='abcdefgh'
print(name[0:3:1])  #步长默认为1
>>>abc        #前三

print(name[0:])
>>>abcdefgh  #全部

print(name[0:-1])  
>>>abcdefg   #倒一(从起始位置到结束位置前)

(2)字符串常用操作

<1>find()

检查字符串中是否包含子字符串

返回子字符串首字母所在下标,不存在返回-1

str1='hello world'

#打印各个元素下标
for i in range(len(str1)):
    print(str1[i],'=%d' %i,end='  ')
>>>h =0  e =1  l =2  l =3  o =4    =5  w =6  o =7  r =8  l =9  d =10

ret1=str1.find('world')
print(ret1)
>>>6
ret2=str1.find('wod')
print(ret2)
>>>-1
<2>index()

同find(),但是index()没有找到时报错

ret3=str1.index('world')
print(ret2)
#正常返回下标>>>6
#没查到时候 程序崩溃
<3>count()

返回子字符串的个数

print(str1.count('o'))
>>>3
<4>replace()
ss='hello world hello'

ss_1=ss.replace('hello','nihao',1)
print(ss_1)

>>>nihao world hello

ss_2=ss.replace('hello','nihao',2)
print(ss_2)

>>>nihao world nihao

#不改变原有字符串 返回替换后的字符串
#不写数字 全部替换
#数字不能超过总的个数
<5>split()

通过指定的分隔符对字符串进行切片

参数num为个数

返回一个列表list

na='hello-world-nihao-world-by-python'
li=na.split('-')  #不写参数  
print(li)
>>>
['hello', 'world', 'nihao', 'world', 'by', 'python']

li2=na.split('-',1)
print(li2)
# ---->
['hello', 'world-nihao-world-by-python']  #分出前1份

li3=na.split('-',2)
print(li3)
---->
['hello', 'world', 'nihao-world-by-python']  #分出前两份

li4=na.split('l')
print(li4)
---->
['he', '', 'o-wor', 'd-nihao-wor', 'd-by-python'] #l划分
<6>capitalize

字符串首字母变大写 其他为小写

na='dbjasdjaksjhjJSKADJAkd'
print(na.capitalize())
>>>
Dbjasdjaksjhjjskadjakd
<7>title()

标题化 使所有的单词首字母大写

na='hello world by pyhton'
print(na.title())
>>>Hello World By Pyhton
<8>startswith()

判断字符串是否以'xxx'开头

返回bool值 True False

print(na.startswith('he'))
>>>True
<9>endswith()

同startswith,结尾是否以什么结尾

返回bool值

<10>lower

将所有大写转为小写

<11>upper

将所有的小写转成大写

<12>ljust

left 左对齐 然后补齐

str1='asdasdas'
str2=str1.ljust(30,'-')
print(str2)
>>> asdasdas----------------------
<13>rjust

right 右对齐 补齐

<14>center

中间对齐 两端补齐

<15>lstrip

截掉字符串左边的空格或某些指定字符

str1.lstrip() 截掉空格

str1.lstrip('6') 截掉左边的6

str1.lstrip(' h') 截掉左边的空格和h

str1.lstrip('h ') 同上 不考虑顺序
<16>rstrip

从右侧截掉

同lstrip()

<17>strip

左右都删 默认空格

<18>rfind()

从右往左查 返回最后一次(第一次找到的位置)出现的位置

虽然从右往左找 但是 返回的是从左往右算的下标

没有匹配返回-1

<19>rindex

没有检索到会崩溃

<20>partition

分割 返回元组

返回三元元组 分成三份 中间 两端

<21>rpartition

同partition 从右边开始找子字符串

返回三元元组

<22>splitlines

根据进行分割

返回list

<23>isalpha

判断是否由字母组成

返回bool

<24>isdigit

判断是否为数字组成

返回bool

<25>isalnum

是否为数字和字母组合

<26>issapce

判断是否为空格

<27>join()
str1='-'
li=['1','2','3']
ret=str1.join(li)
print(ret)
>>>
1-2-3
posted @ 2021-02-20 14:18  Zhou_DE_blogs  阅读(63)  评论(0)    收藏  举报