码迷,mamicode.com
首页 > 其他好文 > 详细

字符串类型内置方法

时间:2019-05-08 09:39:53      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:默认   art   一个   pytho   首字母   alpha   大项目   tar   成员   

# 字符串类型内置方法

# 字符串类型

## 用途:名字,兴趣,爱好

## 定义方式

 

```python
name1 = ‘nick‘
name2 = "nick"
name3 = """
nick
"""
name4 = ‘‘‘
nick
‘‘‘
```

# 常用操作和内置方法

## 优先掌握

### 按索引取值

```python
#1 按索引取值
zhuping_info_list = [‘zhuping‘,190,140,["piao","feiji","2333"]]
print(zhuping_info_list[0])
zhuping_info = ‘zhuping 190 140 ["piao","feiji","2333"]‘
print(zhuping_info[6])
```

```
zhuping
g
```

### 切片(顾头不顾尾),步长的正负控制行进的方向,往左还是往右

```python
print(zhuping_info[:])
print(zhuping_info[0:7])
print(zhuping_info[0:7:2])# 2是步长
print(zhuping_info[0:7:-1])
print(zhuping_info[-1:-3])
print(zhuping_info[-1:-3:-1])
print(zhuping_info[-3:-1])
print(zhuping_info[-3:])
```

```
zhuping 190 140 ["piao","feiji","2333"]
zhuping
zuig
```

?

```
]"
3"
3"]
```

### 长度

```python
print(len(zhuping_info))

```

```
39
```

### 成员运算 in/not in

```python
print(‘zhuping‘ in zhuping_info)
print(‘nick‘ in zhuping_info)


```

```
True
False

```

### 移除空白strip 默认空格

```python
abc = ‘ a abc a‘
print(abc)
print(abc.strip())# 移除左右两端
print(abc.strip(‘a‘))# 移除左右两端的a

```

```
a abc a
a abc a
a abc

```

### 切分 split

```python
zhuping_info = ‘zhuping 190 140 ["piao","feiji","2333"]‘
print(zhuping_info.split(‘ ‘))
print(zhuping_info.split(‘ ‘,1))

```

```
[‘zhuping‘, ‘190‘, ‘140‘, ‘["piao","feiji","2333"]‘]
[‘zhuping‘, ‘190 140 ["piao","feiji","2333"]‘]

```

### 循环

```python
zhuping_info = ‘zhuping 190 140 ["piao","feiji","2333"]‘
for i in zhuping_info:
print(i)

```

```
z
h
u
p
i
n
g

1
9
0

1
4
0

[
"
p
i
a
o
"
,
"
f
e
i
j
i
"
,
"
2
3
3
3
"
]

```

## 需要掌握(本周必须掌握)

### lstrip&rstrip

```python
zhuping_info = ‘&&&&&&&zhuping 190 140["piao","feiji","23333"]&&&&&&&&‘
print(zhuping_info.lstrip(‘&‘))# 左
print(zhuping_info.rstrip(‘&‘))# 右
```

```
zhuping 190 140["piao","feiji","23333"]&&&&&&&&
&&&&&&&zhuping 190 140["piao","feiji","23333"]
```

### lower & upper

```python
print(zhuping_info.lower())# 小写
print(zhuping_info.upper())# 大写
```

```
&&&&&&&zhuping 190 140["piao","feiji","23333"]&&&&&&&&
&&&&&&&ZHUPING 190 140["PIAO","FEIJI","23333"]&&&&&&&&
```

### startswith&endswith

```python
zhuping_info = ‘&&&&&&ZhuPing 190 140 ["Piao","Feiji","2333"]&&&&&&‘
print(zhuping_info.startswith(‘&&&&&&Z‘))#判断开头结尾
print(zhuping_info.endswith(‘&&&&&&Z‘))
```

```
True
False
```

### rsplit

```python
print(zhuping_info.rsplit(‘ ‘))
print(zhuping_info.rsplit(‘ ‘,1))

```

```
[‘&&&&&&ZhuPing‘, ‘190‘, ‘140‘, ‘["Piao","Feiji","2333"]&&&&&&‘]
[‘&&&&&&ZhuPing 190 140‘, ‘["Piao","Feiji","2333"]&&&&&&‘]
```

### join

```python
zhuping_info_split = zhuping_info.rsplit(‘ ‘)
print(‘,‘.join(zhuping_info_split))


```

```
&&&&&&ZhuPing,190,140,["Piao","Feiji","2333"]&&&&&&

```

### replace 替换

```python
print(zhuping_info.replace(‘ZhuPing‘,‘ruixing‘).replace(‘Feiji‘,‘BigFeiji‘))

```

```
&&&&&&ruixing 190 140 ["Piao","BigFeiji","2333"]&&&&&&

```

### isdigit 判断是否为数字

```python
print(‘10asfdsdsfdsd‘.isdigit())
print(‘10‘.isdigit())

```

```
False
True

```

## 了解

### find|rfind|index|rindex|count

```python
ruixing_info = ‘RUIxing feiji BIGFEIJI feiji feiji huojimian‘
# 这里用0和-1 代表true和false
print(ruixing_info.find(‘RUIxing‘)) # 从左边开始找
print(ruixing_info.find(‘RUIxing1‘))
print(ruixing_info.rfind(‘RUIxing‘))# 从右边开始找(常用于大项目中节省时间)
print(ruixing_info.rfind(‘RUIxing1‘))

```

```
0
-1
0
-1

```

 

```python
print(ruixing_info.index(‘RUIxing‘)) # (索引)
# print(ruixing_info.index(‘RUIxing1‘)) # 错
print(ruixing_info.rindex(‘RUIxing‘)) # (从右开始找)
# print(ruixing_info.rindex(‘RUIxing1‘)) # 错
print(ruixing_info.count(‘feiji‘)) # 总计数量
print(ruixing_info.count(‘BIGFEIJI‘))

```

```
0
0
3
1
```

### center|ljust|rjust|zfill

```python
print(‘nick‘.center(50, ‘-‘)) # 一共从头到尾字符50个 当左右两边差1时 默认右边多1
print(‘nic‘.center(50, ‘-‘))
print(‘nick‘.ljust(50, ‘0‘)) # 最左边
print(‘nick‘.rjust(50, ‘0‘)) #最右边
print(‘nick‘.zfill(50))
```

```
-----------------------nick-----------------------
-----------------------nic------------------------
nick0000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000nick
0000000000000000000000000000000000000000000000nick
```

### expandtabs

```python
print(‘\tNiCk‘.expandtabs(2)) # \t 空格 \n 转行
print(‘\tNiCk\n‘.expandtabs(32))
print(‘\tNiCk‘.expandtabs(8))
```

```
NiCk
NiCk

NiCk
```

### captalize|swapcase|title

```python
print(‘nick nicK‘.capitalize()) # 第一个字母大写
print(‘nick nicK‘.swapcase()) # 全大写
print(‘nick nicK‘.title()) # 每个单词首字母大写
```

```
Nick nick
NICK NICk
Nick Nick
```

## is系列

```python
‘nick‘.isdigit()
print(‘nick‘.isalpha()) #判断是否为字母
print(‘10‘.isalpha())
```

```
True
False
```

##
存一个值or多个值:一个值
有序or无序(有索引为有序,否则为无序):有序
可变or不可变:不可变

```python
name = ‘nick‘
print(id(name))
name += ‘handsome‘
print(id(name))
```

```
74655520
75307432
```

### 仅作了解

```python
print(‘D:\飞秋数据\视频+课件\Python基础nick\tday 02‘)
print(f‘D:\飞秋数据\视频+课件\Python基础nick\\tday 02{name}‘)
print(r‘D:\飞秋数据\视频+课件\Python基础nick\tday 02‘)
print(type(‘D:\飞秋数据\视频+课件\Python基础nick\\tday 02‘))
print(type(u‘D:\飞秋数据\视频+课件\Python基础nick\\tday 02‘))
print(type(b‘D:\\\xe9\xa3\x9e\xe7\xa7‘))
```

字符串类型内置方法

标签:默认   art   一个   pytho   首字母   alpha   大项目   tar   成员   

原文地址:https://www.cnblogs.com/zrx19960128/p/10829358.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!