Python Strings

String character analysis

Strings are sequences of characters, and analyzing their content is a common programming task. This involves examining the individual characters within a string to determine their properties, such as whether they are alphabetic, numeric, or whitespace. Python provides a rich set of built-in string methods that simplify this process.

By understanding these methods, you can efficiently validate user input, process text data, and perform various other string-related operations. This chapter will explore the essential string methods for character analysis and provide practical examples to illustrate their usage.


1. isalpha(): Check if all characters are alphabetic

The isalpha() method returns True if all characters in the string are alphabetic (letters). It returns False if there are any non-alphabetic characters, including spaces and punctuation.

Syntax:

string.isalpha()

Example:

text = "Hello"
print(text.isalpha())  # Output: True

text_with_space = "Hello World"
print(text_with_space.isalpha())  # Output: False (contains space)


2. isdigit(): Check if all characters are digits

The isdigit() method returns True if all characters in the string are digits (0–9). It returns False if any character is not a digit.

Syntax: 

string.isdigit()

Example:

text = "12345"
print(text.isdigit())  # Output: True

text_with_letters = "123abc"
print(text_with_letters.isdigit())  # Output: False (contains letters)


3. isalnum(): Check if all characters are alphanumeric

The isalnum() method returns True if all characters in the string are either alphabetic or digits (i.e., no spaces or special characters). It returns False otherwise.

Syntax:

string.isalnum()

Example:

text = "Hello123"
print(text.isalnum())  # Output: True

text_with_space = "Hello 123"
print(text_with_space.isalnum())  # Output: False (contains space)


4. isspace(): Check if all characters are whitespace

The isspace() method returns True if all characters in the string are whitespace characters (spaces, tabs, newlines, etc.). It returns False if any non-whitespace characters are present.

Syntax:

string.isspace()

Example:

text = "   "  # Contains only spaces
print(text.isspace())  # Output: True

text_with_word = "Hello"
print(text_with_word.isspace())  # Output: False (contains non-whitespace characters)


5. islower(): Check if all characters are lowercase

The islower() method returns True if all alphabetic characters in the string are lowercase. It returns False if there are uppercase letters or if the string contains no alphabetic characters.

Syntax:

string.islower()

Example:

text = "hello"
print(text.islower())  # Output: True

text_with_upper = "Hello"
print(text.islower())  # Output: False (contains uppercase letters)


6. isupper(): Check if all characters are uppercase

The isupper() method returns True if all alphabetic characters in the string are uppercase. It returns False if there are lowercase letters or if the string contains no alphabetic characters.

Syntax:

string.isupper()


Example:

text = "HELLO"
print(text.isupper())  # Output: True

text_with_lower = "HELLO world"
print(text.isupper())  # Output: False (contains lowercase letters)


7. istitle(): Check if string is in title case

The istitle() method returns True if the string follows the title case format, meaning the first letter of each word is capitalized and the remaining characters are lowercase.

Syntax:

string.istitle()

Example:

text = "Hello World"
print(text.istitle())  # Output: True

text_with_mixed_case = "hello World"
print(text.istitle())  # Output: False (first letter of "hello" is lowercase)


8. startswith(): Check if the string starts with a specific substring

The startswith() method returns True if the string starts with the specified substring. It returns False otherwise.

Syntax:

string.startswith(substring, start, end)

  • substring: The substring you want to check.
  • start (optional): The starting index for the check.
  • end (optional): The ending index for the check.

Example:

text = "Hello, World!"
print(text.startswith("Hello"))  # Output: True
print(text.startswith("World"))  # Output: False


9. endswith(): Check if the string ends with a specific substring

The endswith() method returns True if the string ends with the specified substring. It returns False otherwise.

Syntax:

string.endswith(substring, start, end)
 
  • substring: The substring you want to check.
  • start (optional): The starting index for the check.
end (optional): The ending index for the check.

Example:

text = "Hello, World!"
print(text.endswith("World!"))  # Output: True
print(text.endswith("Hello"))   # Output: False


Summary:

These methods are very useful for validating or processing strings based on their content, such as ensuring that a user input consists only of digits or checking if a filename has the correct extension.

It's time to take a quiz!

Test your knowledge and see what you've just learned.

Which Python method checks if all characters in a string are alphabetic?

isalnum()
isalpha()
isdigit()
Check Answer

Which Python method checks if all characters in a string are digits?

isalpha()
isdigit()
isalnum()
Check Answer

Which Python method checks if all characters in a string are alphanumeric?

isalpha()
isdigit()
isalnum()
Check Answer

Which Python method checks if all characters in a string are lowercase?

islower()
istitle()
isupper()
Check Answer

Which Python method checks if a string starts with a specific substring?

startswith()
endswith()
isspace()
Check Answer