zoukankan      html  css  js  c++  java
  • 字符串常见操作19个操作

    # coding:utf-8
    # 字符串常见操作
    # 1.find
    
    # find:返回查找字符串的下标位置。如果返回-1,代表得是没有查找到该字符串。
    # 'rfind'是从右往左。
    
    # username = 'hello zhiliao'
    # index = username.find('zhiliao')
    # print(index)
    # 结果为6
    
    # if index > 0:
    # 	print(index)
    # else:
    # 	print('您要查找的字符串不存在')
    
    
    # 2.index:和'find'非常类似。
    # 只不过当查找不到这个字符串时,不是返回-1而是抛出一个异常。
    # 'rindex'是从右边开始查找
    
    # username = 'hello zhiliao'
    # index = username.index('zhiliao')
    # print(index)
    
    
    
    # 3.len:获取字符串字符的长度  ps:空格也会被统计入内     //函数
    # username = 'zhiliao'
    # length = len(username)
    # print(length)
    
    
    # 4.count:用来获取子字符串在原来字符串中出现的次数
    # username = 'zhiliao python python'
    # temp = username.count('python')
    # print(temp)
    
    
    # # 5.replace:/新创建/一个字符串,把原来的字符串中的某个字符串替换为你想要的字符串
    
    # username = 'zhiliao python python'
    # new_username = username.replace('python','ketang')
    # print(new_username)
    
    # 6.split:按照给定的字符串进行分割。返回的是一个列表
    # [' zhiliao','python','ketang']
    # username = 'zhiliaoxpythonxketang'
    # username_list = username.split('x')
    # print(username_list)
    
    # 7.startswith  :判断一个字符串是否以某个字符串开始
    # username = 'zhiliao ketang'
    # result   = username.startswith('zhiliao')
    # print(result)
    
    
    
    
    # url = 'http://www.baidu.com'
    # result = url.startswith('http')
    # if result:
    # 	print('这是一个网址')
    # else:
    # 	print('这不是一个网址')
    
    
    # 8.endswith:判断一个字符串是否以某个字符串结束
    # username = 'zhiliao ketang'
    # result = username.endswith('ketang')
    # print(result)
    
    # file_name = 'xxx.jpg'
    # if file_name.endswith('jpg') or file_name.endswith('gif') or file_name.
    # endswith('png'):
    # 	print('这是一个图片文件')
    # else:
    # 	print('这不是一个图片文件')
    
    
    # 9.lower  将字符串全部改成小写
    # username = 'ZHILIAO KETANG'
    # new_username = username.lower()
    # print(username)
    # print(new_username)
    
    
    # captcha_server = 'uF4D'
    # captcha_user = 'uf4d'
    # if captcha_server.lower() == captcha_user.lower():
    # 	print('输入验证码正确')
    # else:
    # 	print('验证码错误')
    
    
    # 10.upper  将字符串全部改成大写
    
    # 同lower用法
    
     
    # 11.strip  将字符串左右的空格全部去掉
    
    # username = '      zhiliao     '
    # print(len(username))
    # new_username = username.strip()
    
    # print(len(new_username))
    
    
    # telephone = '  15296686333 '
    # telephone_strip = telephone.strip()
    # print(telephone_strip)
    # print(len(telephone_strip))
    
    # 12.lstrip:删除字符串左边的空格
    
    
    
    # 13.rstrip 删除字符串右边的空格
    
    
    
    # 注意:当 username = '   zhiliao ketang ' 想要删除所有空格这时候利用strip就做不到了
    # 所以使用replace 把空格替换为空的字符串
    
    # username = '   zhiliao ketang '
    # new_username = username.replace(' ','')
    # print(new_username)
    
    # 这个时候就实现了删除所有的空格
    
    
    # 14.partition   从str出现的第一个位置起,把字符串分割成三部分,本身和前面的和后面的
    
    # username = 'python zhiliao python'
    # username_tuple = username.partition('zhiliao')
    # print(username_tuple)
    
    
    
    
    # 15.isalnum  如果字符串中至少有一个字符并且所有字符都是数字或字母则返回true  否则返回false
    
    # username = 'zhiliao'
    # result = username.isalnum()
    # print(result)
    
    # 16.isalpha: 如果字符中至少有一个字符并且所有字符都是字母则返回true 否则 false
    # 同上isalnum 用法
    
    # 17.isdigit:如果字符串中只有数字则返回true 否则false
    # 同上isalnum 用法
    
    # 18.isspace:如果字符串中只有空格则返回true 否则false
    # 同上isalnum 用法
    # 19.format:格式化字符串

    # name = 'zhiliao'
    # age = 18
    # greet = 'my name is %s,my age is %d'%(name,age)


    # 1.format函数最简单的使用方式
    # name = 'zhiliao'
    # age = 18
    # greet = 'my name is {},my age is {}'.format(name,age)
    # print(greet)


    # 2.format函数使用位置i参数的方式

    # name = 'zhiliao'
    # age = 1name = 'zhiliao'
    # age = 18
    # greet = 'my name is %s,my age is %d'%(name,age)name = 'zhiliao'
    # print(greet)


    # 3.关键字参数 xx=xxx
    # name = 'zhiliao'
    # age = 18
    # greet = 'my name is {username},my username is {username},my age is{age1}'.format(username = name,age1 = age)
    # print(greet)


    # 查询所有的函数
    # all_methods = dir(str)
    # print (all_methods)
  • 相关阅读:
    SQLite常用SQL语句
    delphi设计浮动窗口
    在Delphi中使用键盘勾子获取键盘输入(译--5月7日)
    Delphi制作软键盘
    Delphi格式化输出函数(1): Format
    《你不知道的 CSS》之等比例缩放的盒子
    请用心练完这16个webpack小例子
    JavaScript高级内容笔记:原型链、继承、执行上下文、作用域链、闭包
    表格组件神器:bootstrap table详细使用指南
    玩转JavaScript正则表达式
  • 原文地址:https://www.cnblogs.com/lsswudi/p/11173347.html
Copyright © 2011-2022 走看看