Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Suchen und Messen | Zeichenketten
Datentypen in Python

bookSuchen und Messen

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.

12345
# Checking if a user's bio mentions specific skills user_bio = "Experienced in Python programming and data analysis." print("Python" in user_bio) # True → the bio mentions Python print("Java" not in user_bio) # True → Java is not listed as a skill
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.
12345678
# Searching for keywords inside a product description description = "This brand new bracelet is made from recycled materials." print(description.find("bra")) # 5 → first occurrence of "bra" print(description.rfind("bra")) # 15 → only one "bra" found print(description.find("gold")) # -1 → not found, returns -1 print(description.index("bra")) # 5 → same as find(), but raises an error if not found print(description.index("gold")) # ValueError → "gold" not in the text
copy

Counting Occurrences

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

12345
# Counting occurrences of words or letters in a customer review review = "Amazing banana smoothie with banana slices on top!" print(review.count("banana")) # 2 → the word appears twice print(review.count("a")) # 8 → letter 'a' appears multiple times
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.

12345
# Checking if the uploaded file has the correct name and format uploaded_file = "report_final.pdf" print(uploaded_file.startswith("report")) # True → file name starts correctly print(uploaded_file.endswith(".pdf")) # True → valid file format for upload
copy

Case-Insensitive Checks

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

12345
# Checking a user's message for a polite greeting user_message = "Hello, team! Let's start the meeting." print("hello" in user_message.lower()) # True → greeting detected print(user_message.lower().startswith("hello")) # True → message begins with "hello"
copy

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain the difference between find and index methods?

How do I make my string searches case-insensitive?

What happens if the substring is not found when using these methods?

bookSuchen und Messen

Swipe um das Menü anzuzeigen

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.

12345
# Checking if a user's bio mentions specific skills user_bio = "Experienced in Python programming and data analysis." print("Python" in user_bio) # True → the bio mentions Python print("Java" not in user_bio) # True → Java is not listed as a skill
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.
12345678
# Searching for keywords inside a product description description = "This brand new bracelet is made from recycled materials." print(description.find("bra")) # 5 → first occurrence of "bra" print(description.rfind("bra")) # 15 → only one "bra" found print(description.find("gold")) # -1 → not found, returns -1 print(description.index("bra")) # 5 → same as find(), but raises an error if not found print(description.index("gold")) # ValueError → "gold" not in the text
copy

Counting Occurrences

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

12345
# Counting occurrences of words or letters in a customer review review = "Amazing banana smoothie with banana slices on top!" print(review.count("banana")) # 2 → the word appears twice print(review.count("a")) # 8 → letter 'a' appears multiple times
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.

12345
# Checking if the uploaded file has the correct name and format uploaded_file = "report_final.pdf" print(uploaded_file.startswith("report")) # True → file name starts correctly print(uploaded_file.endswith(".pdf")) # True → valid file format for upload
copy

Case-Insensitive Checks

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

12345
# Checking a user's message for a polite greeting user_message = "Hello, team! Let's start the meeting." print("hello" in user_message.lower()) # True → greeting detected print(user_message.lower().startswith("hello")) # True → message begins with "hello"
copy

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 3
some-alt