Python开发笔记之-字符串函数
2023-09-14 08:57:35 时间
1.首字母大写
>>> s = 'yuanzhumuban' >>> s.capitalize() 'yuanzhumuban'
>>> s = 'my name is yuanzhumuban, age is 20' >>> s 'my name is yuanzhumuban, age is 20' >>> s.replace( '20', '30' ) 'my name is yuanzhumuban, age is 30' >>> s 'my name is yuanzhumuban, age is 20' >>>
查帮助:
>>> help( str.replace )
如果是面向过程的函数用法,直接help( 函数名 ),如help( abs )
replace(...) S.replace(old, new[, count]) -> string Return a copy of string S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
接受3个参数,第一个需要替换的字符,第二个用什么字符去替换,第三个替换的次数,如果不传,默认全部替换
str = '121212' str.replace( '1', 'g' ) 'g2g2g2' str.replace( '1', 'g', 1 ) 'g21212' str.replace( '1', 'g', 2 ) 'g2g212' str.replace( '1', 'g', 3 ) 'g2g2g2'
1 >>> ip='127.0.0.1' 2 >>> ip 3 '127.0.0.1' 4 >>> ip.split( '.' ) 5 ['127', '0', '0', '1'] 6 >>> ip.split( '.', 1 ) 7 ['127', '0.0.1'] 8 >>> ip.split( '.', 2 ) 9 ['127', '0', '0.1'] 10 >>> ip.split( '.', 3 ) 11 ['127', '0', '0', '1'] 12 >>>
1 >>> import string 2 >>> help( string.capitalize ) 3 4 >>> s = 'ghostwu' 5 >>> string.capitalize( s ) 6 'Ghostwu'
1 >>> import string 2 >>> s = 'my name is ghostwu, age is 20' 3 >>> string.replace( s, '20', '30' ) 4 'my name is ghostwu, age is 30' 5 >>> ip 6 '127.0.0.1' 7 >>> string.split( ip, '.' ) 8 ['127', '0', '0', '1'] 9 >>> string.split( ip, '.', 1 ) 10 ['127', '0.0.1'] 11 >>>
相关文章
- python 生成数组_Python创建数组「建议收藏」
- python字符串转化列表_Python列表到字符串的转换[通俗易懂]
- python jieba库_Python jieba库的使用说明「建议收藏」
- 全网最全python爬虫精进
- python中dtype的使用规范_Python numpy.dtype() 使用实例
- 神经网络学习笔记1——BP神经网络原理到编程实现(matlab,python)[通俗易懂]
- python scrapy 爬虫实例_scrapy爬虫完整实例
- 【Python】python文件打开方式详解——a、a+、r+、w+、rb、rt区别[通俗易懂]
- 【说站】python判断两个集合是否相等
- 全连接神经网络的原理及Python实现
- python lambda表达式举例_Python中lambda表达式[通俗易懂]
- selenium自动化测试实战基于python_初级java工程师要求
- 力扣刷题笔记--14. 最长公共前缀 python解法
- Python选择结构IF语句_Python自学第五节
- [Python私活案例]24行代码,轻松赚取400元,运用Selenium爬取39万条数据
- Python 函数递归教程
- 通过代理服务器访问FTP的Python代码详解编程语言
- Python高级特性——学习笔记详解编程语言
- python工具——Mimesis详解编程语言
- Python操作Redis实现数据持久化(python操作redis)
- Python与MongoDB 无缝连接(python连接mongodb)
- MySQL与Python的协同运行:一种全新的开发体验(mysql与python)
- Python中使用MySQL数据库的操作技巧(mysql_python)
- 企业AI架构师佟达:无处不在的Python
- python和shell实现的校验IP地址合法性脚本分享