字符串

1、形式

  单引号括起来的字符串:'hello'

  双引号括起来的字符串:"Hello"

  三引号括起来的字符串:'''hello'''(三单引号),"""hello"""(三双引号)

  三引号括起来的字符串可以换行

2、下标(索引)

  从左往右,下标从0开始

  从右往右,下标从-1开始

1 a = 'abc' 
2 print(a[0])  ## 输出结果: a
3 print(a[-1]) ## 输出结果: c

 

3、切片-- 相当于字符串的截取

  语法:字符串[起始索引:结束索引:步长]  --- 不包含结束索引的字符

     列表、元祖、字符串都有切片操作!!

 

  格式:     info[开始索引:结束索引]
      不包括结束索引的字符

1 info = 'life is short, you need pyhton '
2 print(info[0:4]) 
3 print(info[5:]) ## 5到结束字符
4 print(info[:10]) # 开始到10 位置
5 print(info[1:10:2]) ## 2表示步长
6 print(info[::2]) ##隔一个取一个
7 print(info[::-1]) ## 倒序!!!

 字符串的常用操作:

  无论对字符串进行怎样的操作,原字符串一定是不变的!!一般是生成了新的字符串

  字符串的不可变性 !!

   1、strip()   ----  去除空格

1 a = ' abc def '
2 print(a.strip())  ## 去除全部空格
3 print(a.lstrip()) ## 去除左边的空格
4 print(a.rstrip()) ## 去除右边的空格

  2、upper、lower --- 大小写转换

1 b = 'abCDE'
2 print(a.lower())  ## 输出结果是:abcde
3 print(a.upper()) ## 输出的结果:ABCDE
4 
5 ##判断是否是大写或小写:
6 isupper()和islower()

  3、endswith()    startswith() --- 判断以xx开头或者以xx结尾

1 b= 'ASD_234'
2 print(b.endswith(‘234’)) ##输出结果是: True
3 print(b.startswith('AS')) ##输出结果是:True

  4、join()   字符串的拼接

1 print(''.join(['hello','world'])) ##输出的结果是:helloworld
2 
3 ##更加常用的拼接方法:
4 a  = 'abc'
5 b  = 'efgr'
6 print(a+b) ## 输出的结果是:abcefgr

  5、各种常用判断数字、字母、空格

## 判断是否都是数字 
print('123'.isdigit())  
print('123'.isdecimal())
print('123'.isnumeric())

## 判断是否是空格
print(' '.isapace()) ## True

## 判断是否都是字符组成
print('abcdf'.isalpha())

## 判断是否是有数字和字符组成
print('fff123'.isalnum())

总结:以上的几个方法都可以配合for循环,遍历之后来一个个判断所有字符的情况,用于统计字符的个数啥的

  6、split() ----- 分割

  分割后的结果不包含分割符,结果返回一个列表

1 name = 'I love you'
2 print(name.split(' ')) ## 输出的结果是:['I', 'love', 'you']
3 
4 ## 若是分割字符不存在,就直接将字符串整体给列表
5 print(name.split('x')) ##输出的结果是:['I love you']
1 小例子:假设输入:10*20  求计算结果
2 shuzi = '10-20'
3 if shuzi.find('*') !=-1:     ##找到了*
4     l = shuzi.split('*')     ## 分割,返回的结果是:l = ['10'.'20']
5     v = int(l[0])*int(l[1])
6     print(V)                 ##输出的结果是:200

  还有一个分割:splitlines()--- 按照换行符进行分割!!

  7、replace()  ----- 替换

    替换之后,生成了一个新的字符串,原来的字符串不变

1 ## 默认替换所有指定字符
2 info = 'life is shorts'
3 print(info.replace('is',was))   ##输出:life was shorts
4 
5 ## 指定替换次数
6 print(info.replace('s','S',1))   ##输出:life is Shorts 

  8、find()  ----- 查找

  如果存在,就返回第一个字符的索引;

  如果不存在,就返回-1

 1 info = 'life is short, you need pyhton '
 2 print(info.find('is'))
 3 print(info.find('iii'))
 4 print(info.find('is',10,20)) ##指定范围
 5 
 6 ##index() 也是查找,若是不存在,会报异常
 7 print(info.index('sss'))
 8 ##指定范围
 9 print(info.index('is',4,20))
10 
11 # ##获取指定的内容出现的次数
12 # ## 不存在返回0次
13 ## 也可以指定范围
14 print(info.count('s'))
15 print(info.count('s',10,15))

  8、format() ----- 格式化字符串

    format函数中,使用{}充当格式化操作符

1 print('{},{}'.format('chuhao',20))        ##chuhao,20
2 print('{1},{0}'.format('chuhao',20))      ##20,chuhao
3 print('{1},{0},{1}'.format('chuhao',20))  ##20,chuhao,20

 9、capitalize:整个字符串的首字母大写

 1 info = 'life is short' 2 print(info.capitalize())##输出:Life is short 

4、字符串的编码

## python3 中使用的是unicode编码
## 编码,相当由于字符的一个编号

## 常用的编码
## ASCII 字符占1个字节 0--127
## unicode 字符占2个字节 0--65535
## UTF-8 英文占1个字节,中文占3个字节
## GB2312 国标 -- 简体中文 中文占2个字节
## GBK 对gb2312的扩展,扩展繁体-- Windows GBK编码
 1 ## 转化为指定编码的字节
 2 ## unicode --->utf-8
 3 print('中国'.encode('UTF-8'))
 4 ##  b'\xe4\xb8\xad\xe6\x96\x87'
 5 print('中国'.encode('GBK'))
 6 ##  b'\xd6\xd0\xb9\xfa'
 7 
 8 print('abc'.encode('GBK'))
 9 ### b'abc'
10 
11 ##将字节根据指定编码,转为unicode字符串
12 print(b'\xe4\xb8\xad\xe5\x9b\xbd'.decode('UTF-8'))
13 
14 ##将字符串表示为uncode
15 print(u'abc')

 

posted on 2018-03-05 19:35  虫子小孩  阅读(236)  评论(0编辑  收藏  举报