【44.1】字符串内建函数

#coding=utf-8
# find  find("abc",2,10)
string_example = "abcdefg hijklmn opq rst uvw xyz xyz now you see"
find = string_example.find("opq",0,-1)
print find


#index  和 find 一样  只是找不到就报错, find 就返回 -1

index = string_example.index("opq",0,-1)
print index

#count 是返回统计某字符的个数
count = string_example.count("opq",0,-1)
print count



#replace 字符替换
replace = string_example.replace("e","E",2)
#  old new count(替换次数)
print replace

#split 切片 通过指定的分隔符来切片
#string_example.split(sep=None,maxsplit=-1)  sep 是分隔符,默认所有空格,换行(\n),制表符(\t)等,maxsplit分割次数
split = string_example.split( )
print split
split = string_example.split("n")
print split
split = string_example.split("n",1)
print split

#capitalize  字符转换,首字符变大写,后面的字符变小写
capitalize = string_example.capitalize()
print capitalize

#title 将字符串 标题化  字符串中的每一个单词都将首字母变大写,后面的字母变小写
title = string_example.title()
print title


#startswith 用于检查字符串是否以指定的字符串开头,结果为True False,若指定start  end 参数值,就在指定的范围检查

startswith = string_example.startswith("h",8,-1)
print startswith

#endswith 用于检查字符串是否以指定的字符串结束

endswith = string_example.endswith("fg",2,7)
print endswith

#ljust 左对齐
#str.ljust(50,"*")  50是字符串长度,如果字符串没有50个字符,默认填充空格,如果指定填充* 则填充*
ljust = string_example.ljust(100,"%")
print ljust

#rjust  右对齐
rjust = string_example.rjust(70,"#")
print rjust

#upper 将字符串中的小写字母 变为大写字母
upper =  string_example.upper()
print upper


#center 居中显示
#str.center(50,"#")  字符串总的宽度为50 ,原字符串居中显示,两边默认填充空格,如指定#,则由#填充

center =string_example.center(100,"*")
print center

#lstrip 用于截取左边的空格(默认)或指定字符
lstrip = string_example.lstrip("abc")
print lstrip

#rstrip 用于截取右边的空格(默认)或指定字符
rstrip = string_example.rstrip("see")
print rstrip

#strip 用于截取头尾的空格(默认)或指定字符
strip = string_example.strip("a")
print strip

 

posted @ 2019-02-22 17:19  科学小怪癖  阅读(129)  评论(0)    收藏  举报