1.首字母大写

>>> s = 'yuanzhumuban'
>>> s.capitalize()
'yuanzhumuban'

    2.replace,替换

>>> 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'

  3.split:切割:

 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 >>>

4.用string模块,用法如下:

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开发笔记之-字符串函数的更多相关文章

  1. python开发笔记之zip()函数用法详解

    今天分享一篇关于python下的zip()函数用法. zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素按顺序组合成一个tuple,每个tuple中包含的是原 ...

  2. Python学习笔记之常用函数及说明

    Python学习笔记之常用函数及说明 俗话说"好记性不如烂笔头",老祖宗们几千年总结出来的东西还是有些道理的,所以,常用的东西也要记下来,不记不知道,一记吓一跳,乖乖,函数咋这么多 ...

  3. python开发笔记-通过xml快捷获取数据

    今天在做下python开发笔记之如何通过xml快捷获取数据,下面以调取nltk语料库为例: import nltk nltk.download() showing info https://raw.g ...

  4. python开发_python中字符串string操作

    在python中,对于字符串string的操作,我们有必要了解一下,这样在我们的以后的开发中会给我们带来很多方便 下面是我学习的笔记: #python-string #python中的字符串用单引号' ...

  5. Python开发——数据类型【字符串】

    字符串定义 字符串是一个有序的字符的集合,用于存储和表示基本的文本信息 在Python中加了引号的字符,都被认为是字符串! 单引号.双引号.多引号之间的区别? 答案:单双引号没有区别 多引号的作用? ...

  6. Python学习笔记010——匿名函数lambda

    1 语法 my_lambda = lambda arg1, arg2 : arg1 + arg2 + 1 arg1.arg2:参数 arg1 + arg2 + 1 :表达式 2 描述 匿名函数不需要r ...

  7. Python 学习笔记 之 03 - 函数总结

    函数总结    最基本的一种代码抽象的方式.    定义函数    使用def语句进行定义, return进行函数返回.    一旦执行导return,函数就执行完毕.    即使函数未指定retur ...

  8. Python初学笔记之字符串

    一.字符串的定义 字符串是就一堆字符,可以使用""(双引号).''(单引号)来创建. 1 one_str = "定义字符串" 字符串内容中包含引号时,可以使用转 ...

  9. Python学习笔记(Ⅱ)——循环/选择/函数

    一.循环结构 python中提供了for循环和while循环两种操作,没有do……while语句. 1.for循环: 与其他语言中for循环的常见的写法如for (int i=0;i<10;i+ ...

随机推荐

  1. sorted内置函数

    对List.Dict进行排序,Python提供了两个方法 --------------------------------sorted--------------------------------- ...

  2. spring AOP注解实现

    一.什么是AOP 引用一下维基百科的定义 面向切面的程序设计(Aspect-oriented programming,AOP,又译作面向方面的程序设计.剖面导向程序设计)是计算机科学中的一种程序设计思 ...

  3. centos7安装php7.3

    安装php7.3 CentOS/RHEL 7.x: yum install epel-release yum install http://rpms.remirepo.net/enterprise/r ...

  4. Linux远程管理命令

    关机\重启 shutdown 选项 时间 参数 -r 重启 例子: shutdown 1分钟后关机 shutdown now 立刻关机 shutdown –r now 立即重启 shutdown 20 ...

  5. Spring MVC异常处理代码完整实例

    Spring MVC异常处理流程: 提供构造方法传值: 配置异常处理器的bean

  6. [洛谷P5377][THUPC2019]鸽鸽的分割

    题目大意:有一个圆,圆上有$n$个点,将这几个点两两连接,问最多分成几部分 题解:发现这相当于一个平面图,由欧拉公式得($F$为平面分割块数,$E$为平面图边数,$V$为平面图点数):$$F=E-V+ ...

  7. Shell变量一览

    Shell变量一览 $# Shell命令的参数个数 $$ Shell本身的进程ID $! Shell最后运行的后台进程的进程ID $? Shell最后运行的命令的退出码(返回值) $- Shell使用 ...

  8. How to read request body in a asp.net core webapi controller?

    原文 How to read request body in a asp.net core webapi controller? A clearer solution, works in ASP.Ne ...

  9. iTextSharp 不适用模板 代码拼接PDF

    /// <summary> /// 打印移库单 /// </summary> /// <param name="guid"></param ...

  10. 【转载】C#中List集合使用RemoveRange方法移除指定索引开始的一段元素

    在C#的List集合操作中,移除集合中的元素可以使用Remove方法和RemoveAt方法,这两个方法都是进行单个List集合元素的移除,其实List集合中还有个RemoveRange方法来移除一整段 ...