字符串str的使用方法
2024-10-16 04:39:22
参考:https://www.cnblogs.com/cookie1026/p/6048092.html
#!/usr/bin/env python
# -*- coding:utf- -*-
s = 'abcd'
ss = 'this is cash'
print s.ljust(,'w')#返回一个原字符串右用字符串0填充左对齐并宽度7的新字符串
print s.rjust(,'-')
print s.center(,'*')#表示原字符串居中两边填充宽度的用法
print ss.center(,' ') print format(ss,'.3')
本文主要介绍了字符串的使用,可以进行字符串切片、字符串长度统计、查找、替换、大小写转换等。
打印结果如下:
#!/usr/bin/env python
# -*- coding:utf- -*-
s = 'aBcdkwwqerD'
ss = '12 this is cash'
print ss.find('th') #检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-。
print ss.find('is')
print ss.find('it') print s.upper(),'--->upper方法' #将小写字母转换成大写字母
print s.lower(),'--->lower方法' #将大写字母转换成小写字母
print ss.isdigit(),'--->isdigit方法' #判断字符串如果是数字返回真True,不是返回假False
print s.startswith('ab'),'--->startswith方法' #判断字符串是否是以abc开头如果是返回真,否则返回假
print s.endswith('D'),'--->endswith方法' #判断字符串是否以yz结尾是返回真,否则返回假 print s.partition("k") #生成一个分隔符k,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。
打印结果:
最新文章
- swift3.0:associatedtype
- ajax onblur 用法
- Eclipse中user library包管理
- [转]C/C++中的memset
- jquery plugins —— datatables 增加行号
- VIM 及其插件使用快捷键汇总
- Topcoder SRM 639 (Div.2)
- Effective C++:条款35:考虑virtual函数以外的其它选择
- HDU 4034 Graph(Floyd变形——逆向判断)
- 常见的web测试功能点测试思路
- Windows 与Office 镜像的区别
- spring事务源码分析结合mybatis源码(三)
- 一个可以配置阴影方向和颜色的类 CardView 控件 SCardView
- 0003 - 基于xml的Spring Bean 的创建过程
- animation 老动画
- report源码分析——report_handle和report_server和report_catcher
- LambdaAOP
- 计算Python运行时间
- LeetCode题解之Merge Two Sorted Lists
- ZT pthread_cleanup_push()/pthread_cleanup_pop()的详解
热门文章
- CSS盒子模型(Box Model)
- c/c++ 继承与多态 子类隐藏父类的同名非虚函数
- Task.Wait and “Inlining”
- 我的第一个python web开发框架(26)——定制ORM(二)
- Redis学习笔记(2)——Redis的下载安装部署
- Hadoop Compatibility in Flink
- centos7下kubernetes(13。kubernetes-探讨service IP)
- 22 python 初学(类,面向对象)
- 检测web界面不能访问后重启
- Visual Studio中Image Watch的使用