python之字符串详细版
ch1 = 'a'
print("ASCII码值为:\t", ord(ch1))
print(ord('a') - ord('A'))
print("数字代表的字母:\t", chr(98))
str1 = "we can read of things that happend 5000 years ago,where people first to write."
str2 = "we can READ OF THINGS that happend 5000 years ago,where PEOPLE FIRST TO WRITE."
print("第一个字符串:\t", str1)
print("第一个字符串:\t", str1, end=";")
print("\n")
print(id(str1))
print(id(str1))
print(id(str2))
str3 = "but there are some parts of the world where even now people cannot write."
print("第一个字符串:\t", str3)
# 测试字符串长度
print(len(str1))
print(len(str3))
# 数字转字符串
num01 = 3.1415926
print(num01)
print(type(num01))
num2string = str(num01)
print(num2string)
print(type(num2string))
# 字符串拼接
print(str1 + str3)
# 从控制台读取数据
# console_s=input("请输入一串文字:\t")
# print("显示结果为:\t",console_s)
print("最大字符为:\t", max(str1))
print("最大字符为:\t", max(str3))
print("$" * 30)
# 打印字符串
# 方法1
# print(str1)
# print(str3)
# 方法2
for i in range(0, len(str1)):
print(str1[i], end="\t")
# 方法3
for ch in str1:
print(ch)
print("\n")
# 字符串截取
print("倒数第二个字符为:\t", str1[-2])
print("倒数第二个字符为:\t", str1[-2 + len(str1)])
print("部分随意截取\t", str1[1:15])
print("部分随意截取\t", str1[:15])
print("部分随意截取\t", str1[1:])
print("部分随意截取\t", str1[1:-1])
# 字符串复制
print(str3 * 3)
# 测试子字符串存在性
print("read of" in str1)
print("read about" in str1)
print("*" * 30)
# 字符串比较
print(str1 == str3)
print(str1 != str3)
print(str1 > str3)
print(str1 >= str3)
print(str1 < str3)
print(str1 <= str3)
print("*" * 60)
print("hello world".isalnum())
print("hello".isalpha())
print("hello world".isalpha())
print(str1.upper())
print("helloworld".isspace())
print("hello world".isidentifier())
print(str3.lower())
print("#" * 70)
# 字串的子串搜索
print(str1.endswith("write."))
print(str1.endswith("write"))
print(str1.startswith("we"))
print(str1.startswith("our"))
print(str1.find("read"))
print(str1.find("read of"))
print("hello world".rfind("wor"))
print("hello world".count("o"))
# 字符串字母大小写
print("hello WorLd".capitalize())
print("hello WorLd".title())
print("hello WorLd".lower())
print("hello WorLd".upper())
print("hello WorLd".swapcase())
print(str1.replace("of","about"))
# 字符串格式化
print("Welcome".center(16))
print("Welcome".ljust(16))
print("Welcome".rjust(16))
posted on 2020-09-10 10:59 Indian_Mysore 阅读(76) 评论(0) 收藏 举报
浙公网安备 33010602011771号