python中string字符串模块

这篇博客介绍了Python早期版本中的string模块,尽管现在已更新到3.x,了解历史背景仍有价值。内容展示了如何使用字符串代替string模块的函数,并演示了字符串的大小写转换、splitlines()函数以及将字符串转化为数字等操作。

    现在python人家都更新到3.x了,我看了一本python1.5的string模块讲解,其实看看这些也不错,知道这些都是怎么来的,先把早期的演示演示,压压惊,冷静冷静,这个string模块就是这么用的。

 

import  string
str = "I love you ?"

print 'upper'              
print string.upper(str)

print 'lower'
print string.lower(str)

print 'split'
print string.split(str)

这个感觉不太爽啊,于是将其改为字符串代替string模块函数,下面这个作用一看就懂把,不用多说

str = "I love you ?"

print 'upper'       #转化成大写          
print str.upper()

print 'lower'       #转化成小写
<pre name="code" class="python">print str.lower()

print 'split'       #默认空格分割,返回列表
print str.split()

print 'join'        # -用连接列表中的东西
print '+'.join(str.split())    

print 'replace'     #替换字符串
print  str.replace('love','hate')
  
print 'find'        #找到返回下标,找不到返回-1
print str.find('love'),str.find('hate')

print 'count'       #统计指定字符串出现的次数
print str.count('o')


输出结果


upper
I LOVE YOU ?
lower
i love you ?
split
['I', 'love', 'you', '?']
join
I+love+you+?
replace
I hate you ?
find
2 -1
count
2


在来点其他的字符串操作的函数

大小写转化

>>> str = 'i love you'
>>> str.capitalize()  #首字母大写
'I love you'
>>> str.swapcase()    #大小写互换
'I LOVE YOU'
>>> str.title()       #以分隔符为标记,首字符为大写,其余为小写
'I Love You'
>>> 

字符串条件判断

    >>> str='0123'
    >>> str.isalnum()  #是否全是字母和数字,并至少有一个字符 
    True
    >>> str.isdigit()   #是否全是数字,并至少有一个字符
    True

    >>> str='abcd'
    >>> str.isalnum()
    True
    >>> str.isalpha()   #是否全是字母,并至少有一个字符
    True
    >>> str.islower()   #是否全是小写,当全是小写和数字一起时候,也判断为True 
    True

    >>> str='abcd0123'
    >>> str.islower()   #同上
    True
    >>> str.isalnum()   
    True

    >>> str=' '
    >>> str.isspace()    #是否全是空白字符,并至少有一个字符
    True
    >>> str='ABC'
    >>> str.isupper()    #是否全是大写,当全是大写和数字一起时候,也判断为True
    True
    >>> str='Abb Acc'
    >>> str.istitle()    #所有单词字首都是大写,标题
    True

    >>> str='string learn'
    >>> str.startswith('str') #判断字符串以'str'开头
    True
    >>> str.endswith('arn')   #判读字符串以'arn'结尾
    True

<pre name="code" class="python"><span style="color:#0000CC;">></span><span style="color:#0000CC;">></span><span style="color:#0000CC;">></span> str<span style="color:#0000CC;">=</span><span style="color:#FF00FF;">'string lEARn</span>'
>>> str.strip('n') #删除字符串首尾匹配的字符,通常用于默认删除回车符'string lEAR'>>> str.lstrip('n') #左匹配'string lEARn'>>> str.rstrip('n') #右匹配'string lEAR'>>>>>> str=' tab'>>> str.expandtabs() #把制表符转为空格' tab'>>> str.expandtabs(2) #指定空格数' tab'


Python splitlines() 按照行分隔,返回一个包含各行作为元素的列表


虽然是酱紫的,但是存在即是合理,这句话还是很相信的,string模块可以用来将字符串转化成数字


#coding:utf-8

import string

print int("7758")
print string.atoi("7758")
print string.atoi("7758",16)
print string.atoi("7758",8)

print float("521")
print string.atof("1")
print string.atof("1.23e5")



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值