首页 > 其他 > 详细

3.8字符串

时间:2018-02-07 14:29:04      阅读:224      评论:0      收藏:0      [点我收藏+]
# -*-coding:utf-8 -*- __date__ = ‘2018/2/7 11:11‘ __author__ = ‘xiaojiaxin‘ __file_name__ = ‘string1‘ #创建字符串 str1="hello world!" str2="nihao!" print(str1) print(str2) print(str2*4) print(str1[1:]) #判断成员关系 print("h" in str1)

hello world!
nihao!
nihao!nihao!nihao!nihao!
ello world!
True

#字符串拼接
str5="welcome!"
str3=‘-----‘.join([str1,str2])    #推荐此方法,字符串的内置方法   iterable可迭代的
str4=‘>>>‘.join([str1,str2,str5])   #join()只能接一个参数!
print(str1)
print(str2)
print(str1+str2)
print(str3)
print(str4)

hello world!
nihao!
hello world!nihao!
hello world!-----nihao!
hello world!>>>nihao!>>>welcome!

#string 的其他内置方法
str10="welcome here!"
print(str10.capitalize())    #首字母大写
print(str10.count(‘e‘))
print(len(str10.center(50,‘-‘)))   #居中,总共50个字符
print(str10.casefold())
print(str10.encode(encoding=‘utf-8‘))   #大量用,大量出问题
print(str10.endswith("!"))#是否以某个数结尾
#True
print(str10.startswith("w"))    #常用,用于文件处理
#True

Welcome here!
4
50
welcome here!
b‘welcome here!‘
True
True

str11="welcome\t users!"
print(str11)
print(str11.expandtabs(tabsize=16))   #一个tab空16格
print(str11.find("l"))    #查找第一个匹配元素的索引值,并将索引值返回
str12="welcome users {name},it is {age} years old!"
print(str12.format(name=‘jiaxin‘,age=18))     #格式化输出,非常重要
print(str12.format_map({"name":"jiaxin1","age":"19"})) #格式化输出
print(str12.index("l"))    #和find方法相同
#print(str12.index("9999"))   #找不到报错
print(str12.find("999"))    #不报错,找不到返回-1
print(str12.isalnum())      #判断是否为数字
print(str12.isdecimal())    #判断是否是十进制
print(‘abc‘.isalpha())     #判断是否都为字母
print(str12.islower())      #是否都为小写
print(‘  ‘.isspace())      #是否都为空格
print(‘1adco‘.isidentifier())   #是否可以当标识用
print("MY name".swapcase())     #大小写颠倒
print("my tile".ljust(50,‘-‘))
print(‘ my title ‘.strip())    #很重要!!!!开头结尾的空格,换行符都去掉
print(‘ my title    ‘.lstrip())
print(‘ my title    ‘.rstrip())
print(‘ my  title y‘.replace("y",‘1‘,2))   #很重要!!2表示替换两次
print("mytitle".rfind(‘t‘))     #从右开始找引索
print("my title".split(‘--‘))
#[‘my title‘]   #变成了列表,非常重要 ,把字符串变成了列表
#将列表变成字符串,用join方法
print(" ".join(("my","TITLE")))
print(type(" ".join(("my","TITLE"))))

print( ‘>>‘.join((‘1‘,‘2‘)))   #很重要!!
#1>>2

print("i am robot".title())  #变成标题模式

welcome users!
welcome users!
2
welcome users jiaxin,it is 18 years old!
welcome users jiaxin1,it is 19 years old!
2
-1
False
False
True
True
True
False
my NAME
my tile-------------------------------------------
my title
my title
my title
m1 title 1
4
[‘my title‘]
my TITLE
<class ‘str‘>
1>>2
I Am Robot

3.8字符串

原文:http://blog.51cto.com/10777193/2069817

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!