python基础----day1(字符串的魔法)

#expandtabs, 断句  20

1 test = "username\temail\tpassword\nlishijie\[email protected]\t123"
2 v = test.expandtabs(20)
3 print(v)

           (20)表示username是8个字母,用空格补全剩下的20个(即12个空格)

           \t 表示行内      \n  分行

 

#isalpha 全是字母、汉字表示  true

test = "lishijie"
v = test.isalpha()
print(v)

 

#当前输入是否是数字

1 test = ""
2 v1 = test.isdecimal()
3 v2 = test.isdigit()
4 v3 = test.isnumeric()
5 print(v1,v2,v3)

 

#是否存在不可显现的字符(eg:\t 制表符  \n 换行符)

1 test = "djhsfih\tkjjdgh"
2 v = test.isprintable()
3 print(v)

 

#判断是否全部是空格

test = "     "
v = test.isspace()
print()

 

#判断是否是标题(全部大写)

test = "i am a talent"
v1 = test.istitle()
print(v1)
v2 = test.title()
print(v2)
v3 = v2.istitle()
print(v3)

 

####按字符串中的每一个元素按照指定分隔符进行拼接

test = "床前明月光"
print(test)
t = ' '
v = t.join(test)
print(v)

 

 

#设置宽度,并将内容居中

#20代指总长度

#  *  空白未知填充,一个字符,可有可无

test = "lishijie"
v = test.center(20,"*")
print(v)

#字放左边

test = "lishijie"
v = test.ljust(20,"*")
print(v)

#字放右边

test = "lishijie"
v = test.rjust(20,"*")
print(v)

 

 

 

# islower判断是否是小写,  lower  变成小写

test = "LIshijie"
v1 = test.islower()
v2 = test.lower()
print(v1,v2)

isupper判断全部是否是大写,   upper变成大写

test = "LIshijie"
v1 = test.isupper()
v2 = test.upper()
print(v1,v2)

 

replece  替换     (括号里2,表示替换前两个)

test = "lishijie"
v = test.replace("shijie","qihao")
print(v)
test = "lishijielishijielishijie"
v = test.replace("shijie","qihao",2)
print(v)

 

 #########################################7个基本魔法###############################

 join

split

find

strip

upper

lower

replace

#######################################其他############################

#索引,下标    (从0开始)获取字符串中的某一个字符

test =  "lishijie"
v = test[3]
print(v)

  

#切片:取的一个范围     [1:3]表示大于等于1,小于3

test = "lishijie"
v = test[1:2]
print(v)

 

# len获取当前字符串中由几个字符组成

test = "lishijie"
v = len(test)
print(v)

 

#for 循环

test = "刘翔宇是傻逼"
for item in test:
    print(item)

 

 

#######################在字符串中,记住11个魔法##########################################

*****字符串一旦创建,不可修改
     一旦创建或者拼接,都会造成重新生成字符串
 
 
#将文字对应的索引打印出来
test = input(">>>")
for item in range (0,len(test)):
    print(item,test[item])

 

你可能感兴趣的:(python基础----day1(字符串的魔法))