码迷,mamicode.com
首页 > 编程语言 > 详细

python基础--字符串

时间:2018-03-05 20:36:25      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:art   下标   index   short   family   开头   dex   可变   分割   

字符串

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

 

python基础--字符串

标签:art   下标   index   short   family   开头   dex   可变   分割   

原文地址:https://www.cnblogs.com/dyl01/p/8510397.html

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