Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Searching and Measuring | Strings
Data Types in Python

bookSearching and Measuring

Once you can read pieces of a string, the next step is to ask questions about its contents: "does it contain this?", "where is it?", "how many times?", "does it start/end with…?".

Membership

Use in to check if a substring appears anywhere in a string. It returns a Boolean.

123
s = "Python programming" print("gram" in s) # True print("Java" not in s) # True
copy

Finding Positions

  • find(sub) returns the starting index of the first match, or -1 if not found;
  • rfind(sub) searches from the right and returns the last match index (or -1);
  • index(sub) is like find, but raises ValueError if the substring is missing.
12345
s = "abracadabra" print(s.find("bra")) # 1 print(s.rfind("bra")) # 8 print(s.find("xyz")) # -1 print(s.index("xyz")) # ValueError
copy

Counting Occurrences

count(sub) returns how many non-overlapping times the substring appears.

123
s = "banana" print(s.count("a")) # 3 print(s.count("na")) # 2
copy

Starts and Ends

To check a string's start or end, use startswith or endswith; these are clearer and safer than slicing when you only need a yes/no answer.

123
fname = "report_final.pdf" print(fname.startswith("report")) # True print(fname.endswith(".pdf")) # True
copy

Case-insensitive checks (simple and practical)

String methods are case-sensitive. For case-insensitive search, normalize both sides with .lower() (or .upper()).

123
msg = "Hello, World!" print("hello" in msg.lower()) # True print(msg.lower().startswith("hello")) # True
copy
Note
Note

We already used len() in the previous chapter to measure length; here we focus on content checks and locations.

1. What will this code output?

2. Which statement does not raise an error when the substring is missing?

3. Given s = "Banana", which expression returns True case-insensitively for checking the prefix "ba"?

question mark

What will this code output?

Select the correct answer

question mark

Which statement does not raise an error when the substring is missing?

Select the correct answer

question mark

Given s = "Banana", which expression returns True case-insensitively for checking the prefix "ba"?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 5

bookSearching and Measuring

Swipe to show menu

Once you can read pieces of a string, the next step is to ask questions about its contents: "does it contain this?", "where is it?", "how many times?", "does it start/end with…?".

Membership

Use in to check if a substring appears anywhere in a string. It returns a Boolean.

123
s = "Python programming" print("gram" in s) # True print("Java" not in s) # True
copy

Finding Positions

  • find(sub) returns the starting index of the first match, or -1 if not found;
  • rfind(sub) searches from the right and returns the last match index (or -1);
  • index(sub) is like find, but raises ValueError if the substring is missing.
12345
s = "abracadabra" print(s.find("bra")) # 1 print(s.rfind("bra")) # 8 print(s.find("xyz")) # -1 print(s.index("xyz")) # ValueError
copy

Counting Occurrences

count(sub) returns how many non-overlapping times the substring appears.

123
s = "banana" print(s.count("a")) # 3 print(s.count("na")) # 2
copy

Starts and Ends

To check a string's start or end, use startswith or endswith; these are clearer and safer than slicing when you only need a yes/no answer.

123
fname = "report_final.pdf" print(fname.startswith("report")) # True print(fname.endswith(".pdf")) # True
copy

Case-insensitive checks (simple and practical)

String methods are case-sensitive. For case-insensitive search, normalize both sides with .lower() (or .upper()).

123
msg = "Hello, World!" print("hello" in msg.lower()) # True print(msg.lower().startswith("hello")) # True
copy
Note
Note

We already used len() in the previous chapter to measure length; here we focus on content checks and locations.

1. What will this code output?

2. Which statement does not raise an error when the substring is missing?

3. Given s = "Banana", which expression returns True case-insensitively for checking the prefix "ba"?

question mark

What will this code output?

Select the correct answer

question mark

Which statement does not raise an error when the substring is missing?

Select the correct answer

question mark

Given s = "Banana", which expression returns True case-insensitively for checking the prefix "ba"?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3
some-alt