Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Sammenligne Strenger | Interaksjoner Mellom Ulike Datatyper
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Datatyper i Python

bookSammenligne Strenger

String comparisons in Python are case-sensitive by default. If user input may vary in capitalization or include extra spaces, normalize first, then compare. A simple and reliable recipe is to trim whitespace and standardize case before any equality or prefix/suffix checks.

Case-Sensitive vs Case-Insensitive

By default, "Apple" == "apple" is False. To ignore case, normalize both sides.

12345
# Comparing two email addresses entered with different letter cases email_saved = "Support@Codefinity.com" email_entered = "support@codefinity.COM" print(email_saved.lower() == email_entered.lower()) # True → emails match regardless of case
copy

casefold() is a stronger, international-friendly variant of lower() and is a better default for case-insensitive comparisons.

12345
# Comparing international usernames regardless of letter case username_db = "straße" username_input = "STRASSE" print(username_db.casefold() == username_input.casefold()) # True → matches even with special characters
copy

Trim and Compare

Users often add spaces by accident. Remove leading and trailing whitespace before comparing.

12345
# Validating a user's role input from a form user_input = " admin " required_role = "ADMIN" print(user_input.strip().casefold() == required_role.casefold()) # True → matches after cleanup and case normalization
copy

Prefix and Suffix Checks

Use startswith and endswith. For case-insensitive checks, normalize first.

123456789
# Checking if the uploaded document has the correct format and name uploaded_file = "Report_Final.PDF" # Validate file type (case-insensitive) print(uploaded_file.lower().endswith(".pdf")) # True → valid PDF file # Validate file name prefix (e.g., all reports start with "rep") required_prefix = "rep" print(uploaded_file.strip().casefold().startswith(required_prefix.casefold())) # True → matches prefix ignoring case
copy

Consistent Normalization Pipeline

Pick a simple, repeatable order:

  1. Strip outer whitespace with .strip();
  2. Standardize case with .casefold() (or .lower() if you prefer);
  3. Then use ==, in, startswith, endswith, or other checks.

1. Which line performs a case-insensitive equality check correctly?

2. Given s = " Hello ", which expression returns True for a case-insensitive equality check with "hello" ignoring surrounding spaces?

3. Which statement is most accurate for robust case-insensitive comparison?

question mark

Which line performs a case-insensitive equality check correctly?

Select the correct answer

question mark

Given s = " Hello ", which expression returns True for a case-insensitive equality check with "hello" ignoring surrounding spaces?

Select the correct answer

question mark

Which statement is most accurate for robust case-insensitive comparison?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain the difference between lower() and casefold() in more detail?

How do I handle string comparisons for user input in different languages?

Can you show more examples of combining strip(), casefold(), and startswith()?

bookSammenligne Strenger

Sveip for å vise menyen

String comparisons in Python are case-sensitive by default. If user input may vary in capitalization or include extra spaces, normalize first, then compare. A simple and reliable recipe is to trim whitespace and standardize case before any equality or prefix/suffix checks.

Case-Sensitive vs Case-Insensitive

By default, "Apple" == "apple" is False. To ignore case, normalize both sides.

12345
# Comparing two email addresses entered with different letter cases email_saved = "Support@Codefinity.com" email_entered = "support@codefinity.COM" print(email_saved.lower() == email_entered.lower()) # True → emails match regardless of case
copy

casefold() is a stronger, international-friendly variant of lower() and is a better default for case-insensitive comparisons.

12345
# Comparing international usernames regardless of letter case username_db = "straße" username_input = "STRASSE" print(username_db.casefold() == username_input.casefold()) # True → matches even with special characters
copy

Trim and Compare

Users often add spaces by accident. Remove leading and trailing whitespace before comparing.

12345
# Validating a user's role input from a form user_input = " admin " required_role = "ADMIN" print(user_input.strip().casefold() == required_role.casefold()) # True → matches after cleanup and case normalization
copy

Prefix and Suffix Checks

Use startswith and endswith. For case-insensitive checks, normalize first.

123456789
# Checking if the uploaded document has the correct format and name uploaded_file = "Report_Final.PDF" # Validate file type (case-insensitive) print(uploaded_file.lower().endswith(".pdf")) # True → valid PDF file # Validate file name prefix (e.g., all reports start with "rep") required_prefix = "rep" print(uploaded_file.strip().casefold().startswith(required_prefix.casefold())) # True → matches prefix ignoring case
copy

Consistent Normalization Pipeline

Pick a simple, repeatable order:

  1. Strip outer whitespace with .strip();
  2. Standardize case with .casefold() (or .lower() if you prefer);
  3. Then use ==, in, startswith, endswith, or other checks.

1. Which line performs a case-insensitive equality check correctly?

2. Given s = " Hello ", which expression returns True for a case-insensitive equality check with "hello" ignoring surrounding spaces?

3. Which statement is most accurate for robust case-insensitive comparison?

question mark

Which line performs a case-insensitive equality check correctly?

Select the correct answer

question mark

Given s = " Hello ", which expression returns True for a case-insensitive equality check with "hello" ignoring surrounding spaces?

Select the correct answer

question mark

Which statement is most accurate for robust case-insensitive comparison?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 2
some-alt