string字符串常用操作

#-*-coding:utf-8 -*-
'''
Created on 2014年11月5日

@author: Zroad
'''
import sys

#将字符串转换为序列
strList = list("Hello,python!")
print strList

#遍历字符串中的任意一字符
for char in "Hello,python!":
    #print char
    #print char,  #print不换行的处理
    sys.stdout.write(char)
    sys.stdout.flush()
print '\n'

#输出字符串不换行的第二种处理
sys.stdout.write("Hello,world!")
sys.stdout.flush()

#使用map函数遍历字符串中的每个字符
results = map(ord,"hello,python")
print results

#ASC||/Unicode单字符串与码值转换
print ord('a')  #ASC||字符串 ‘a’对应的码值
print chr(97)   #ASC||码97对应的字符


#str()与repr()的区别,str函数针对的是ASC||码
print ord(u'\u2020')  #Unicode字符\u2020对应的码值
print repr(unichr(8224)) #将unicode码值转换为Unicode字符  

#1、判断str/Unicode字符串对象
def isAString(anobj):
    return isinstance(anobj,basestring)


print isAString('Hello,pyton')
print isAString(u'\8338')
print isAString(3990)
print isAString(chr(58))


#2、字符串左、中、右对齐
print "|",'hello,python'.rjust(20,'+') #width参数表示长度,包括对齐的字符串+间隔字符
print '|','hello,python'.ljust(20,'*')
print '|','hello,python1'.center(20,'-') #不加第二个参数默认添加空格


#3、去除字符串两端空格
x = "    hello,python   "
print x.lstrip()+'here'
print x.strip()
print x.rstrip()


y = "here,  hello,python,   here"
print y.strip('hre')  #去除字符串y左右两边的'h'/'r'/'e'字符


#4、字符串合并
strs = ['hello',',','python']
print '/'.join(strs)  #join函数接受字符串列表参数


#使用格式化字符串合并字符串是最优的选择
name = 'Zroad'
number = 100
print "Hello,%s;Go and %d" %(name,number)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值