#字符串拼接 str1 = "Hello" str2 = "World" combined_str = str1 + " " + str2 print(combined_str) #字符串重复 str1 = "Python " repeated_str = str1 * 3 print(repeated_str) #根据字符串 索引取值 str1 = "Hello" char = str1[1] # 索引从0开始 print(char) #索引切片 str1 = "Hello World" slice = str1[0:5] # 切片从0开始,但不包括结束索引 print(slice) # 输出: Hello #索引切片 str1 = "Hello" length = len(str1) print(length) # 输出: 5 #字符串搜索 str1 = "Hello World" found = "World" in str1 print(found) #字符串替换 str1 = "Hello World" replaced = str1.replace("World", "Python") print(replaced) # 输出: Hello Python #字符串切割 str1 = "Hello,World,Python" splitted = str1.split(",") print(splitted) # 输出: ['Hello', 'World', 'Python'] #字符串大小写转换 str1 = "Hello" upper = str1.upper() # 转换为大写 lower = str1.lower() # 转换为小写 print(upper) # 输出: HELLO print(lower) # 输出: hello #去除空格 str1 = " Hello " stripped = str1.strip() # 去除两端空白 print(stripped) # 输出: "Hello" #以什么开头 str1 = " Hello " stripped =startswith("h")# 以什么开头 布尔类型 print(stripped) # 输出: Ture #以什么结尾 str1 = " Hello " stripped =endswith("h")# 以什么结尾 布尔类型 print(stripped) # 输出:False #格式化format str1 = " Hello_{} " s=str1.format("word") print(stripped) #hello_word
标签:python,stripped,str1,print,World,字符串,操作,Hello From: https://www.cnblogs.com/gzy0631/p/18119882