** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **
◆.startswith
.startswith
s = 'My name is Mike. Hi, Mike.'
print(s)
is_start = s.startswith('My')
print(is_start)
is_start = s.startswith('You')
print(is_start)
result
My name is Mike. Hi, Mike.
True
False
With .startswith
, you can check" whether it starts with the specified character (string)? ".
.find
and .rfind
python:.find_and_.rfind
s = 'My name is Mike. Hi, Mike.'
print(s)
print(s.find('Mike'))
print(s.rfind('Mike'))
result
My name is Mike. Hi, Mike.
11
21
In .find
, you can check" what number is the specified character (string)? ".
This time'Mike'appears twice in the text, but .find
looks at the location of the first'Mike'.
In .rfind
, it will be searched from the back.
This time, we are investigating the position of'Mike', which appeared for the second time.
M y n a m e i s M i k e . H i , M i k e .
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
When I write the index, it looks like this.
◆.count
.count
s = 'My name is Mike. Hi, Mike.'
print(s)
print(s.count('Mike'))
result
My name is Mike. Hi, Mike.
2
Use .count
to find out how many times'Mike'appeared in the text.
.capitalize
and .title
.capitalize
s = 'My name is Mike. Hi, Mike.'
print(s)
print(s.capitalize())
print(s.title())
print(s.upper())
print(s.lower())
result
My name is Mike. Hi, Mike.
My name is mike. hi, mike.
My Name Is Mike. Hi, Mike.
MY NAME IS MIKE. HI, MIKE.
my name is mike. hi, mike.
If you use .capitalize
, only the first letter of the sentence will be uppercase and the rest will be lowercase.
If you use .title
, the first letter of each word will be capitalized.
In .upper
, all characters are capitalized.
In .lower
, all characters are in lowercase.
◆.replace
.replace
s = 'My name is Mike. Hi, Mike.'
print(s)
print(s.replace('Mike', 'Nancy'))
result
My name is Mike. Hi, Mike.
My name is Nancy. Hi, Nancy.
With .replace
, you can replace the specified character string with any character string.
Recommended Posts