zoukankan      html  css  js  c++  java
  • Python字符串的简单操作

    数据的操作

    字符串的一些常用操作:

    1

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

    2

    1 # test='hello world'
    2 
    3 # print(test.center(50))      #居中
    4 
    5 # print(test.center(50,'*'))  #居中加填充

    3

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

    4

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

    5

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

    6

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

    7

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

    8

    # test='hello world'
    
    # test_new='*'.join(test)
    
    # print (test_new)        #在两个字符之间加入其它字符

    9

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

    10

    1 # test='HELLO WORLD'
    2 
    3 # print (test.upper())    #小写转换成大写
    4 
    5 # print(test.isupper())   #判断字符串是否为大写

    11

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

    12

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

    字符串其它不常用的方法

     13

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

    14

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

    15

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

    16

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

    17

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

    18

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

    19

    1 # test='Hello world'
    2 
    3 
    4 # print(test.istitle())                     #判断字符串是否已大写字母开头,是则返回True 没有则返回False

    20 

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

    21

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

    22

    1 test='hello world'
    2 
    3 
    4 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

    1 test='hello world'
    2 
    3 for i in test:7
    4 
    5     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)           #字符串相乘(字符串不可以进行减法与除法)
  • 相关阅读:
    vmware中3中网络模式的区别
    常用 Git 命令清单 转
    inotify监控目录变化重启服务器tornado项目
    sed处理url编码解码=== web日志的url处理
    LVM 'Can’t open /dev/sdb1 exclusively. Mounted filesystem?' Problem
    Vimium使用快捷键总结
    sed 引入shell变量
    shell 除法 小数点
    selinux 导致无法启动httpd
    linux查看ssh用户登录日志与操作日志
  • 原文地址:https://www.cnblogs.com/charles1ee/p/6266125.html
Copyright © 2011-2022 走看看