Python-------字符串操作
来源:互联网 发布:free mobile java 编辑:程序博客网 时间:2024/05/21 06:25
# !/usr/bin/env python# -*- coding: utf-8 -*-# Author: Justin Channame = 'my \t name is {name} and i am {year} old'print(name.capitalize())#首字母大写print(name.isdigit())#数字判断print(name.count('a'))#统计字符个数print(name.center(50,'-'))#一共需要打印50个字符,如果不够‘-’补上print(name.ljust(50,'*'))#保证name字串长度为50,如果不够用‘×’右边补上print(name.rjust(50,'*'))#保证name字串长度为50,如果不够用‘×’左边补上print(name.encode())#编码,转换成二进制数print(name.endswith('ld'))#判断是否以'ld'结尾print(name.expandtabs(tabsize=10))#设置tab键转成10个空格print(name.find('old'))#找到’old‘的启始位置print(name.format(name='Justin',year=18))#格式化输出,替换name和yearprint(name.format_map({'name':'Justin','year':12}))#与format类似,传入的是字典print('123'.isalnum())#’123‘是否是阿拉伯数字,如果有一个非阿拉伯数字返回falseprint('afd'.isalpha())#’afd‘是否是阿拉伯字母,如果有一个非阿拉伯字母返回falseprint('1a'.isdecimal())#'la'是否是10进制print('3AB'.isidentifier())#判断是不是一个合法的标识符,即是不是一个合法的变量名,不是数字和非法字符开头print('fafs'.islower())#判断是不是小写、print('fafs'.isupper())#判断是不是大写print('2541.33'.isnumeric())#判断是不是数字,’.‘字符也不行,纯数字print('My name'.istitle())print('My name'.isprintable())#是否可以打印,is tty file or drive file is not printableprint('+'.join(['1','2','3','4']))#用+号把列表的内容隔开print('afKFDS'.lower())#把大写转小写print('afKFDS'.upper())#把小写转大写print('Justin Chan\n'.strip())#去掉空格和回车print('\n Justin Chan \n'.lstrip())#去掉左边的空格和回车print('\n Justin Chan\n'.rstrip())#去掉右边的空格和回车p = str.maketrans('fajufsi','1234567')print('Justin'.translate(p))#有点加密的味道,把'fajufsi'和'1234567'每个字符一一对应起来,传入p,把‘Justin’其中出现的字符替换掉。print('JUSTIN'.replace('USTIN','ustin',1))#替换print('Justin Chan'.rfind('n'))#从左往右查找‘n’,找到最右边的索引。print('Justin Chan'.split('n'))#把'n'当作分隔符,分割'Justin Chan',并输出列表中print('Justin Chan'.swapcase())#字母大小写互换print('justin Chan'.title())#转换为标题,标题首字母大写print('justin Chan'.zfill(15))#长度不够,用zero填充
输出:
My name is {name} and i am {year} oldFalse5-----my name is {name} and i am {year} old------my name is {name} and i am {year} old**********************my name is {name} and i am {year} oldb'my \t name is {name} and i am {year} old'Truemy name is {name} and i am {year} old36my name is Justin and i am 18 oldmy name is Justin and i am 12 oldTrueTrueFalseFalseTrueFalseFalseFalseTrue1+2+3+4afkfdsAFKFDSJustin ChanJustin Chan Justin ChanJ46t7nJustin10['Justi', ' Cha', '']jUSTIN cHANJustin Chan0000justin Chan
字符运算符参与字符运算
可以使用 > < ==参与到字符串的运算,原理是根据字符串中每个字符的ASCII或Unicode码值逐个字符比较大小。
>>> print("abc" < "acc")True
阅读全文
0 0
- Python字符串操作
- Python字符串操作
- Python字符串操作
- python 字符串操作
- python字符串操作
- Python字符串操作
- Python字符串操作[转]
- Python字符串操作
- Python字符串操作
- Python学习---字符串操作
- python 中的字符串操作
- python------字符串连接操作
- python字符串操作
- python 字符串操作
- Python字符串操作
- python字符串操作
- Python字符串操作
- python 中的字符串操作
- 明日(2017.10.30)待整理的文件
- android studio自动整理代码快捷键
- Android文件打开方式,获取相对应type,万能
- Python 习题里程碑
- 【Java】NIO 客户端-服务器 聊天 例子
- Python-------字符串操作
- 杭电oj
- 百度:缩水90亿后,AI商业化能否扭转逆势?
- 未来计算是什么样的?张晓东、丛京生等六大教授这样说 | 未来论坛 2017
- VIPKID项碧波:利用大数据实现个性化教育规模化
- mysql组复制单主模式
- Windows下bat同jar交互
- 十年后,程序员还会有今天的收入吗?
- 1997年,初识互联网,直到2004年家里第一次上网。龟速的网络。