Python 字符串操作函数二

#-*- coding:utf-8 -*-

line = "l want watch movie with you ."

print(line.center(50))
print(line.ljust(50))
print(line.rjust(50))

#center 字符串居中
#ljust 字符串居左
#rjust 字符中居右

#lstrip 删除字符串左边的空白字符
#rstrip 删除字符串右边的空白字符
#strip 删除字符串两端的空白字符

word = "
 	  i am a bird . 
 
 "

print("==lstrip==")
print(word.lstrip())


print("===rstrip==")
print(word.rstrip())


print("==strip==")
print(word.strip())

#空白字符包括空格 /n /t等不可见字符


nword = "hello this a new world ."
print("==partition==")
#partition
print(nword.partition("a"))
#在源字符串中从左向右查找指定字符串,找到指定字符串做切割,分为3部分,指定字符串前,后,其本身,返回值是元组
#如果指定字符串不存在于源字符串中,那么结果由源字符串本身,两个空字符串组成

#rpartition 从右向左查找指定字符串


wline = "hello 
 world"
print("==splitlines==")
print(wline.splitlines())
#splitlines 按照行进行分割,返回一个包含各行作为元素 的列表


print("==isalpha==")
print(wline.isalpha())  #返回false 因为存在空格,转义字符
#isalpha 判断字符串中所有字符是否都是字母,字母返回true,其他返回false


num = "12233"
print("==isdigit==")
print(num.isdigit())
#isdigit 判断字符串中所有字符是否都是数字,是返回true,其他返回false


str="sadfa213"
print("==isalnum==")
print(str.isalnum())
#isalnum 字符串是否只由数字和字母组成,是返回true,其他返回false


space="  "
print("==isspace==")
print(space.isspace())
#isspace 判断字符串中是否只有空格,是返回true,其他返回false


print("==join==")
tstr = "_"
list1 = ["aa","bb"]
print(tstr.join(list1))
#每个字符后面都插入指定的字符,构成一个新的字符串
原文地址:https://www.cnblogs.com/zhanggaofeng/p/9286508.html