Python字符串常用的方法——真心觉得比java,c好用

# Strings have many methods wo can use
rand_string="     life is a beautiful struggle     "
print("去除左边空格:",rand_string.lstrip())
print("去除右边的空格:",rand_string.rstrip())
print("去除两边的空格:",rand_string.strip())
print()
rand_string=rand_string.strip()
print("首字符变为大写:",rand_string.capitalize())
print("全部字母变为大写:",rand_string.upper())
print("全部变为小写:",rand_string.lower())
print("**********列表**********")
stringList=["I","am","a","student"]
print("输出列表:"," ".join(stringList))
randList=rand_string.split()
print("输出字符串变为数组:")
for i in randList:
    print(i)
print()
print("字符串有多少个if;",rand_string.count("if"))
print("字符串is的位置:",rand_string.find("is"))
print("我们把struggle变为journey:",rand_string.replace('struggle','journet'))
print("************对字符串数字进行判断*********")
letter="r"
num="3"
space=" "
print("判断字符串是否为字符:",letter.isalpha())
print("判断字符串是否为字符或者数字:",letter.isalnum())
print("判断字符串是否为数字:",num.isdigit())
print("判断字符串是否为空格:",space.isspace())

猜你喜欢

转载自www.cnblogs.com/lyxcode/p/10908945.html