python之路——字符串操作
来源:互联网 发布:兄弟连nginx视频教程 编辑:程序博客网 时间:2024/06/06 17:39
字符串操作
- 统计字符串中含有指定字符的个数
print('my name is Nick'.count('n'))
print—>:
1
- 首字母大写
print('my name is Nick'.capitalize())
print—>:
My name is nick
- 将字符串居中
1.如果只指定长度,则会使用空格来填充;2.如果给定了填充的字符串,则会使用给定的字符串填充至给定长度
print('my name is Nick'.center(50))print('my name is Nick'.center(50, '*'))
print—>:
my name is Nick
************my name is Nick*************
- 字符串编码
print('my name is Nick'.encode(encoding='utf-8'))
print—>:
b’my name is Nick’
- 判断字符串是否以指定的字符串结尾
print('my name is Nick'.endswith('Nick'))
print—>:
True
- 将制表符转换成给定长度的空格数
print('Nick\t\tJudy'.expandtabs(10))
print—>:
Nick Judy
- 统计字符串中含有的字符个数
print('my name is Nick'.find('n'))
print—>:
3
- 格式化字符串
print('my name is {name} and age is {age}'.format(name='Nick', age=23))
print—>:
my name is Nick and age is 23
- 格式化字符串(以字典的方式)
print(name_map = 'my name is {name} and age is {age}'.format_map({'name': 'Nick', 'age': 23}))
print—>:
my name is Nick and age is 23
- 统计字符所在位置
print('my name is Nick'.index('a'))
print—>:
4
- 判断字符串中是否只含有数字和字母
print('3ag'.isalnum())
print—>:
True
- 判断字符串是否只包含字母
print('myy'.isalpha())
print—>:
True
- 判断字符串是否只包含数字
print('45'.isdecimal())
print—>:
True
- 判断字符串是否可以转换成整数
print('67'.isdigit())
print—>:
True
- 判断字符串是否是合法的标识符
print('my'.isidentifier())
print—>:
True
- 判断字符串是否是小写
print('my name is nick'.islower())
print—>:
True
- 将字符串转换成小写
print('my name is Nick'.lower())
print—>:
my name is nick
- 将字符串从左往右进行填充
print('my name is Nick'.ljust(20, '*'))
print—>:
my name is Nick*****
- 从左边起去除给定的字符
print('nninck'.lstrip('n'))
print—>:
inck
- 将字符串转换成标题
print('my name is Nick'.title())
print—>:
My Name Is Nick
- 将字符串全部转换成大写
print('my name is Nick'.upper())
print—>:
MY NAME IS NICK
- 从左边开始填充数字0至字符串达到给定的长度
print('my name is Nick'.zfill(20))
print—>:
00000my name is Nick
- 翻译字符串
print('my name is Nick'.translate(str.maketrans('aeiou', '12345')))
print—>
my n1m2 3s N3ck
阅读全文
0 0
- python之路——字符串操作
- python之路字符串操作
- Python——字符串操作
- python攻关之字符串操作
- python学习笔记——字符串操作
- python之路——list操作
- python之路——字典操作
- python之路——集合操作
- python之路——文件操作
- python 学习之路(字符串的常用操作)
- Python——字符串格式化与字符串操作
- Python字符串操作之字符大小写转换
- python学习笔记之二:字符串操作
- python字符串的操作——python cookbook
- Python字符串操作之字符串搜索与替换
- Python字符串操作之字符串分割与组合
- Python字符串操作集锦之字符串映射表
- Python字符串操作集锦之字符串编码解码函数
- CSS3 选择器
- 设计模式原则(3):依赖倒置原则
- Android常用工具之对字符串String进行MD5加密
- Python中关于decode、encode以及编码方式
- Android之多线程解析(二)之Runnable、Callable、FutureTask
- python之路——字符串操作
- LibSVM
- 禁用COOKIE后SESSION是如何传递
- 分治法排序
- [bzoj2555][后缀平衡树]SubString
- py thon 多线程(转一篇好文章)
- Codeforces Round #423 A. Restaurant Tables
- FutureTask源码浅析
- 技能树