代码实例
one = "JR"
two = "stronger"
three = "JRs176er"
four = " excellent "
print("两个字符串连接:", one + two)
print(two, "字符串输出N次:", two*4)
print("判断一个字符串或字符是否某个字符串中:")
if ("d" in two) :
print("√")
else:
print("×")
print(two, "输出固定下标为3处的值:", two[3])
print(two, "输出固定下标为-4处的值:", two[-4])
print(two, "输出指定下标范围内的所有值:", two[2:6])
print(two, "小写转换大写:", two.upper())
print(one, "小写转换小写:", one.lower())
print(two, "判断是否全是小写:", two.islower())
print(one, "判断是否全是大写:", one.isupper())
print(three, "判断是否全是字母:", three.isalpha())
print(three, "判断是否以“JRs”开头:", three.startswith("JRs"))
print(three, "判断是否以“er”结束:", three.endswith("er"))
print(three, "把“176”替换成“746”:", three.replace("176", "746"))
print(four, "去两边空格:", four.strip())
print(four, "去左边空格:", four.lstrip())
print(four, "去右边空格:", four.rstrip())
print(four, "的长度:", len(four))
运行结果
两个字符串连接: JRstronger
stronger 字符串输出N次: strongerstrongerstrongerstronger
判断一个字符串或字符是否某个字符串中:
×
stronger 输出固定下标为3处的值: o
stronger 输出固定下标为-4处的值: n
stronger 输出指定下标范围内的所有值: rong
stronger 小写转换大写: STRONGER
JR 小写转换小写: jr
stronger 判断是否全是小写: True
JR 判断是否全是大写: True
JRs176er 判断是否全是字母: False
JRs176er 判断是否以“JRs”开头: True
JRs176er 判断是否以“er”结束: True
JRs176er 把“176”替换成“746”: JRs746er
excellent 去两边空格: excellent
excellent 去左边空格: excellent
excellent 去右边空格: excellent
excellent 的长度: 13
笔记小贴士
【Moment】:每次尽力做好一点。