新浦京81707con > 注册购买 > python字符串函数,python字符串常用内建函数总结

原标题:python字符串函数,python字符串常用内建函数总结

浏览次数:123 时间:2019-05-05

用法表达:

python字符串函数,python字符串

壹,首字母大写

>>> s = 'ghostwu'
>>> s.capitalize()
'Ghostwu'

2,replace,替换

>>> s = 'my name is ghostwu, age is 20'
>>> s
'my name is ghostwu, age is 20'
>>> s.replace( '20', '30' )
'my name is ghostwu, age is 30'
>>> s
'my name is ghostwu, 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.

收受一个参数,第一个须要替换的字符,第一个用什么字符去替换,第七个替换的次数,如若不传,私下认可全体交替

1 >>> str = '121212'
2 >>> str.replace( '1', 'g' )
3 'g2g2g2'
4 >>> str.replace( '1', 'g', 1 )
5 'g21212'
6 >>> str.replace( '1', 'g', 2 )
7 'g2g212'
8 >>> str.replace( '1', 'g', 3 )
9 '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 >>> 

四,用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 >>> 

5,filter:过滤, 利用那个函数,筛选出一个队列的偶数

 1 >>> def isEven( n ):
 2 ...     if n % 2 == 0:
 3 ...             return True
 4 ... 
 5 >>> isEven( 10 )
 6 True
 7 >>> isEven( 3 )
 8 >>> range( 10 )
 9 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
10 >>> l = range( 10 )
11 >>> filter( isEven, l )
12 [0, 2, 4, 6, 8]
13 >>> 

 

一,首字母大写 s = ' ghostwu ' s.capitalize() ' Ghostwu ' 二,replace,替换 s = ' my name is ghostwu, age is 20 ' s ' my name is gho...

Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.

四,用string模块,用法如下:

归来值字母大写转变为小写,大转小

>>> help( str.replace )

a.index

 1 >>> def isEven( n ):
 2 ...     if n % 2 == 0:
 3 ...             return True
 4 ... 
 5 >>> isEven( 10 )
 6 True
 7 >>> isEven( 3 )
 8 >>> range( 10 )
 9 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
10 >>> l = range( 10 )
11 >>> filter( isEven, l )
12 [0, 2, 4, 6, 8]
13 >>> 

Return the number of non-overlapping occurrences of substring sub in
string S[start:end]. Optional arguments start and end are
interpreted as in slice notation.
计算子字符串在钦定字符串中现身的次数

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

将old替换为new

5,filter:过滤, 利用那么些函数,筛选出多少个种类的偶数

实例:

一,首字母大写

Return a copy of the string S converted to lowercase.

 

a.rstrip()

2,replace,替换

Help on method_descriptor:

收受1个参数,第三个供给替换的字符,第一个用哪些字符去替换,第多个替换的次数,倘使不传,暗中同意全体轮换

Start

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.

实例:

>>> s = 'my name is ghostwu, age is 20'
>>> s
'my name is ghostwu, age is 20'
>>> s.replace( '20', '30' )
'my name is ghostwu, age is 30'
>>> s
'my name is ghostwu, age is 20'
>>> 

实例:

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

str.index()

万一是面向进度的函数用法,直接help( 函数名 ),如help( abs )

str.strip

replace(...)
S.replace(old, new[, count]) -> string

rstrip
S.rstrip -> str

3,split:切割

回去值字母大写调换到小写,小写转变到大写

>>> s = 'ghostwu'
>>> s.capitalize()
'Ghostwu'

实例:

1 >>> str = '121212'
2 >>> str.replace( '1', 'g' )
3 'g2g2g2'
4 >>> str.replace( '1', 'g', 1 )
5 'g21212'
6 >>> str.replace( '1', 'g', 2 )
7 'g2g212'
8 >>> str.replace( '1', 'g', 3 )
9 'g2g2g2'

a.find

查帮助

replace
S.replace(old, new[, count]) -> str

a.swapcase()

Start

Help on method_descriptor:

-1

Start

start -- 先河索引,默以为0。

Like S.find() but raise ValueError when the substring is not found.

swapcase
S.swapcase() -> str

a = “sTART”

Help on method_descriptor:

实例:

a = “START”

a.index

a = “start”

除去str字符串末尾的空格,或换行符号

count
S.count(sub[, start[, end]]) -> int

a = “start”

Help on method_descriptor:

b = “a”

2

a = “start”

index
S.index(sub[, start[, end]]) -> int

sTART

实例:

START

Help on method_descriptor:

本文由新浦京81707con发布于注册购买,转载请注明出处:python字符串函数,python字符串常用内建函数总结

关键词: 新浦京81707con 内建 字符串 函数

上一篇:Canvas绘制图形,canvas学习笔记

下一篇:没有了