python的字符串操作1

'''
int
str
bool
list 存储大量的数据,用[]来表示
tuple 元组,不可以发生改变,用()来表示,和C++的元组是一样的
dict 字典,保存键值对,一样可以保持大量的数据,和C++的map一样
set 集合,内部数据不可以重复
'''

 字符串的任何操作都不会改变它本身,所以需要提供另一个字符串来承装

#字符串的截取片段
s1 = input("输入字符串:
")
a = input("输入开始截取的位置:
")
b = input("输入结束截取的位置:
")
a = int(a) - 1
b = int(b)
s2 = s1[a:b]
print(s2)
print(s1[:5]) #默认从开始截取到第5个位置(包括第五个位置)
#字符串首字母大写转化
s1 = "abnddhj"
s2 = s1.capitalize()
print(s1)
print(s2)

#字符串的大小写转化
s3 = "abcABC"
s4 = s3.lower() #字符串的小写转化
s5 = s3.upper() #字符串的大写转化
s6 = s3.swapcase() #大小写互换
print(s3)
print(s4)
print(s5)
print(s6)
#去掉字符串两边的空格
using = input("输入登录用户名").strip() #把输入的字符串去掉空格,只能去掉字符串前后的空格,不能去掉字符串中间的空格
password = input("输入密码").strip() #把输入的密码字符串中的空格去掉,只能去掉字符串前后的空格,不能去掉字符串中间的空格
print(using)
print(password)
if using == "liuwenjie" and password == "123":
    print("登陆成功")
else:
    print("登陆失败")

#去掉字符串两边指定的内容
string = "abcdadcdabcd"
print(string.strip("abcd"))
print(string.strip("abc"))
 
原文地址:https://www.cnblogs.com/boost/p/13229455.html