数据的操作

字符串的一些常用操作:

1

 1 #!/usr/bin/env python
2 # #coding=utf-8
3 #
4 # test='hello world'
5 # print(test.capitalize()) #首字母大写
6 # print (test)
7 #
8 # ###test.capitalize这个参数的运行结果并不会影响test的值。所以下面的print(test)的输出结果还是helloword

2

 # test='hello world'

 # print(test.center(50))      #居中

 # print(test.center(50,'*'))  #居中加填充

3

 # test='hello world'
# print(test.count('l')) #查找字符串中出现的字符次数,有则输出。没有则输出0
# print (test.count('l',0,2)) #查找字符串中出现的字符次数,并设置启始位置与结束位置

4

 # test='hello world'
# print(test.endswith('l')) #判断字符串是以什么字符结尾
# print(test.endswith('d'))
#
# print (test.startswith('d')) #判断字符串是以什么字符开头
# print(test.startswith('h'))

5

 # test='hello world'
# print(test.find('z'))
# print(test.find('o')) #查找字符串中的子字符是否存在,存在则输出字符串序列位置。没有则返回-1
# print(test.find('l',0,7)) #设置起始位置与结束位置,并查找字符

6

 # test='hello world'

 # print(test.index('l'))  #index与find区别:find在查找,找不到则返回-1而index是在已知的情况在进行查找,找不到则报错

7

 # test='hello world'
# print(test.isdigit())
# test1='12345687890'
# test1='aaa1234'
# print (test1.isdigit()) #查看字符串中是否由纯数字组成

8

# test='hello world'

# test_new='*'.join(test)

# print (test_new)        #在两个字符之间加入其它字符

9

 # test='root:x:0:0:root:/root:/bin/bash'
# print(test.split(':'))
# print (test.split(':',maxsplit=1)) #按冒号切割字符。maxsplit=? '?'表示最大分隔几次
#
# test_list=test.split(':')
# print (':'.join(test_list)) #与join相交互调用

10

 # test='HELLO WORLD'

 # print (test.upper())    #小写转换成大写

 # print(test.isupper())   #判断字符串是否为大写

11

 # test='                  hellO world                      x'
# print(test.strip('')) #strip去掉开头与结尾字符。冒号里为空则删除空格
# print(test.strip('x')) #x并不是在开头或者结尾位置,则不删除任何字符 # print(test.rstrip('x')) #去掉右边不去掉左边
# print(test.lstrip(' ')) #去掉左边不去掉右边

12

 # test='hello  world'

 # print(test.replace(' ','',1))     #replace去掉中间的字符,例如两个空格若要后面不加参数则统统替换

字符串其它不常用的方法

13

 # test='hello world'
# test1='hello123world'
# test2='helloworld'
# print(test.isalpha())
# print (test1.isalpha())
# print (test2.isalpha()) #判断字符串是否是纯字母结构,如果是则返回True 否则返回False

14

 # test='hello world'
# test1='type'
# print(test.isidentifier())
# print(test1.isidentifier()) #判断字符串是否为内置标识符

15

 # test='hello world'
# test1="HELLO WORLD"
# print(test.isupper())
# print(test1.isupper()) #判断字符串是否为大写

16

# test='hello world'
# print(test.isprintable()) #判断是否可以被打印 没什么卵用

17

 # test='hello world'
# test1=' '
# print(test.isspace())
# print(test1.isspace()) #判断字符串是否由空格组成

18

 # test='hello world'
# test1=' '
# print(test.islower())
# print(test1.islower()) #判断字符串中是否有字母,有则返回True 没有则返回False

19

 # test='Hello world'

 # print(test.istitle())                     #判断字符串是否已大写字母开头,是则返回True 没有则返回False

20 

 # test='hello world'
# print(test.ljust(20,'*'))
# print (test.rjust(20,'*')) #左对齐填充字符 与右对齐填充字符

21

 # test='''This is first line.
# This is two line.
# This is three line.
# '''
# print (test.splitlines()) #将整段字符串按换行切

22

 test='hello world'

 print (test.zfill(20))              #设置字符串宽度,不足在左侧填零

字符串属于序列类型 。序列又称为有序排列的意思 所以字符也是有序排列的,那么怎么操作呢!各位往下看

切记python计数是从零开始,所以第一个字符为0,第二个字符为1,第三个字符为2,..... 等等。

字符串的索引操作

1

1 # test='hello'
2 # print (test[0]) #切出第一个字符
3 # print(test[4]) #切出第四个字符
4 # print(test[-1]) #负数是从右往左数,-1就是倒数第一个,-2就是倒数第二个等等
5
6 # test='hello'
7 # print(test[1000]) #如果索引超出序列 则报错

字符串的切分操作

2

1 # test='hello world'
2 # print(test[0:3]) #切片切出从0开始到第三个字符 但是在python 顾头不顾尾,即只切到序列2 切记切记!!!
3 # print(test) #对于字符串的任何操作 并不会改变原来的字符串,除非test=test(test[0:3])
4 # print(test[0:]) #从头切到尾,冒号后面不指定序列 将会切到最后
5 # print (test[:3]) #冒号前面如果不指定索引 则从0开始切
6 # print(test[0:5:2]) #从0开始切到5 步长为2,意思是隔一个字符切一个字符
7 # print(test[::-1]) #从后往前切,整个颠倒

字符串的遍历

3

 test='hello world'

 for i in test:7

     print(i)    #将字符串循环遍历出来

变量的解压

4

1 # test='hello'
2 # x,y,z='abc'
3 # print (x)
4 # print (y)
5 # print (z) #变量的解压
6 # x,y,z='abc','defss','xxx'
7 # print (x)
8 # print (y)
9 # print (z)

字符串的一些数学运算

不常用

# meg=''
# print (meg+'hello') #字符串的相加
# meg1='hello'
# print (meg1*10) #字符串相乘(字符串不可以进行减法与除法)

Python字符串的简单操作的更多相关文章

  1. Python字符串的相关操作

    1.大小写转换 判断字符串 s.isalnum() #所有字符都是数字或者字母 s.isalpha() #所有字符都是字母 s.isdigit() #所有字符都是数字 s.islower() #所有字 ...

  2. Python第一天——入门Python(2)字符串的简单操作

    数据的操作 字符串的一些常用操作: 1 1 #!/usr/bin/env python 2 # #coding=utf-8 3 # 4 # test='hello world' 5 # print(t ...

  3. python字符串与文本操作(一)

    1.一个字符串分割为多个字段,但是分隔符 (还有周围的空格) 并不是固定的 #string 对象的split()方法只适应于非常简单的字符串分割情形,它并不允许有 多个分隔符或者是分隔符周围不确定的空 ...

  4. Python字符串的常用操作学习

    >>> name = "I love my job!" >>> name.capitalize() #首字母大写 'I love my job! ...

  5. python字符串和数值操作函数大全(非常全)

    字符串和数值型数字的操作大全 1.反斜杠\的使用规则:一般使用表示续行的操作,可以其他符号相结合组成其他的一些使用符号,转义字符\‘的使用会识别引号,使得字符串中的引号和外面本来的啊引号相区分. (1 ...

  6. python字符串的常见操作

    find: 根据指定字符串获取对应的下标, 如果找不到对应的数据返回-1, 这里的-1表示没有找到数据 my_str = "hello" # find: 根据指定字符串获取对应的下 ...

  7. 关于python字符串连接的操作

    python字符串连接的N种方式 注:本文转自http://www.cnblogs.com/dream397/p/3925436.html 这是一篇不错的文章 故转 python中有很多字符串连接方式 ...

  8. Python 字符串概念和操作

    # 字符串概念:由单个字符串组成的一个集合 # 普通字符串(非原始字符串) str = "abc" print(str) # abc # 原始字符串(前面加r) str = r&q ...

  9. 【代码学习】PYTHON字符串的常见操作

    一.字符串运算符 下表实例变量 a 值为字符串 "Hello",b 变量值为 "Python": 操作符 描述 实例 + 字符串连接 >>>a ...

随机推荐

  1. APP如何发布到Google play 商店

    APP如何发布到Google play 商店?以及有哪些需要注意的点 2015-05-13 10:07 19773人阅读 评论(1) 收藏 举报  分类: iPhone游戏开发(330)  链接:ht ...

  2. 北京Uber优步司机奖励政策(2月17日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. 《Java I/O 从0到1》 - 第Ⅰ滴血 File

    前言 File 类的介绍主要会依据<Java 编程思想>以及官网API .相信大家在日常工作中,肯定会遇到文件流的读取等操作,但是在搜索过程中,并没有找到一个介绍的很简洁明了的文章.因此, ...

  4. android学习九 对话框碎片

    1.android的对话框是异步的,对话框创建后马上执行下面的代码.好处:      a.通过实现对话框的回调方法反馈用户与对话框的交互.    b.能够在代码中清楚对话框.     2.碎片对话框基 ...

  5. uvaoj 101 - The Blocks Problem(vector应用+技巧)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=835&page= ...

  6. Python-S9——Day82-CRM项目实战

    1.权限的概念: 2.RBAC的设计: 3.注册登录用户所有权限到session中: 4.权限的校验: 5.基于中间件的权限校验: 1.权限的概念: 1.1 项目与应用: Project App 1. ...

  7. (C#)代理模式

    1.代理模式 为其他对象提供代理以控制对这个对象的访问. 远程代理:为一个对象在不同的地址空间提供举报代表.这样可以隐藏一个对象在不同地址空间的事实. 虚拟代理:是依据需要创建开销很大的对象.通过它来 ...

  8. java后台接收微信服务号/订阅号消息

    1.申请订阅号(适合个人)或者服务号(适合企业) 微信公众平台 2.填写配置 服务器地址: 需要接收消息 的服务端接口地址 令牌:通话识别码,随便写,后端接收时,使用一样的就可以了. 消息加密秘钥 : ...

  9. git 跟踪分支 远程跟踪分支 学习笔记

    远程跟踪分支相当于一个只读仓库指针,从服务器上获取数据,不可以被本地直接修改. 跟踪分支相当于一个本地指针   用于项目更新和迭代. 1跟踪分支  (tracking  branch)   逻辑示意图 ...

  10. java并发总览