1. String
str1 = "name"
str2="""name
class"""
String output:
name = "Tom"
print(f'My name is {name}')
String input:
name = input("请输入姓名:")
print(f'My name is {name}')
2. Slicing
Slicing refers to the operation of intercepting a part of the operation object. Strings, lists, and tuples all support slice operations.
Syntax:
sequence [start position subscript: end position subscript: step length]
Note:
does not include the data corresponding to the end position subscript. The
step length is the selection interval for both positive and negative integers , and the default step length is 1 The
interval is left closed and right open. The
selection direction must be the same, and the subscript direction is the same as the step direction
str1 = "abcdefg"
print(str1[2:5:1]) # cde
print(str1[2:5]) # cde 默认步长为1
print(str1[:5]) # abcde 默认从0开始步长为1
print(str1[1:]) # bcdefg 默认结束位置为最后一个 步长为0
print(str1[::-1]) # gfedcba 步长为负数为倒序列
print(str1[-4:-1]) # def 最后一个元素的下标为-1
3. Common methods of string
3.1 Find
find(): Check whether a substring is contained in this string, if it returns the subscript at the beginning of the substring, otherwise it returns -1.
Syntax:
string sequence.find(substring, start subscript, end Position subscript)
Note: The
start and end position subscripts can be omitted, which means to search in the entire string sequence
str1 = "hello world,hello python"
print(str1.find("hello")) # 0
print(str1.find("hello",5)) # 12
index(): Detect whether a certain substring is contained in this string, if it returns the subscript at the beginning of the substring, otherwise it will report an exception.
Syntax:
string sequence.index(substring, start position index, end position index )
Note: The
start and end position subscripts can be omitted, which means to search in the entire string sequence
print(str1.index("hello")) # 0
print(str1.index("hello",5)) # 12
count(): count the number of occurrences of a string.
Syntax:
string sequence.count (substring, start position subscript, end position subscript)
Note: the
start and end position subscripts can be omitted, which means to search in the entire string sequence
print(str1.count("hello")) # 2
print(str1.count("hello",5)) # 1
3.2 Modify
replace(): Replace
Syntax:
string sequence.replace (old substring, new substring, number of replacements)
Note:
If the number of occurrences of a substring is found, the number of replacements is the number of occurrences of the substring
print(str1.replace("hello","hi")) # hi world,hi python
print(str1) # 注意:原字符串并未发生修改,str1是不可变类型
split(): Split the string according to the specified character
Syntax:
string sequence.split (split character, num)
Note:
num represents the number of times the split character appears, and the number of data to be returned is num+1
str1 = "hello world,hello python"
list1=str1.split("hello")
list2=str1.split("hello",1)
print(list1) # ['', ' world,', ' python']
print(list2) # ['', ' world,hello python']
join(): Combine strings with one character or string.
Syntax:
character or substring. Join ( sequence of multiple strings)
l = ["hello","world"]
str2 = ",".join(l)
print(str2) # hello,world
capitalize(): convert the first character of the string to uppercase
title(): convert the first letter of each word in
the string to uppercase lower(): convert uppercase in the string to lowercase
upper(): lowercase in the string To uppercase
str1 = "hello,world"
str2 = "Hello,World"
print(str1.capitalize()) # Hello,world
print(str1.title()) # Hello,World
print(str2.lower()) # hello,world
print(str1.upper()) # HELLO,WORLD