字符串操作
评论(0) 浏览量(7210)

# -*- coding: cp936 -*-
#Python3.0中所有字符串都是unicode
name = raw_input("input your name:")#input 用户需要输入"name"
print("hello,"+name)
raw_input("Press <enter>")
print(repr("HelloWorld!"))
print(str("HelloWorld!"))
print(repr("100000000L"))
print(str("100000000L"))
print(repr("100000000L")+repr("hello"))
print("hello,"+"erveryone---"\
                            +"-----niub")#\可以连接两行
print("c\nowhere")  #\n为换行,所以不会输出想象的情况。
print("c\\nowhere") #这样可以正常输出
print(r"c\nowhere") #r""可以避免输入大量\\

#find查找字符串索引,没找到返回-1
str = "123xsdasd"
print(str.find("xs"))
print(str.find("xs",10,24))#提供起始点和结束点
#join
seq = ["1","2","3","4","5"]
seq2 = "+".join(seq)
print(seq)
print(seq2)
#序列转化为字符串
str = "".join(seq)
print(str)
dirs = ["","usr","bin","env"]
print("/".join(dirs))
print("C:"+"\\".join(dirs))
#lower,upper大小写转化 title或capwords首字母大写
print("sdasdDASAS".lower())
print("sdasdDASAS".upper())
print("it's niu b".title())
import string
print(string.capwords("it's niu b"))
#replace替换
print("123123123123123".replace("1","4"))
#split分隔
print("1+2+3+4+5".split("+"))
#strip 去除两侧空格
print("    sd    ".strip())

运行结果:

input your name:zhangsan
hello,zhangsan
Press <enter>
'HelloWorld!'
HelloWorld!
'100000000L'
100000000L
'100000000L''hello'
hello,erveryone--------niub
c
owhere
c\nowhere
c\nowhere
3
-1
['1', '2', '3', '4', '5']
1+2+3+4+5
12345
/usr/bin/env
C:\usr\bin\env
sdasddasas
SDASDDASAS
It'S Niu B
It's Niu B
423423423423423
['1', '2', '3', '4', '5']
sd

没有登录不能评论