Python > Core Python Basics > Fundamental Data Types > Strings (str)

Common String Methods

This snippet demonstrates some of the most commonly used string methods in Python, including case conversion, searching, and splitting.

Case Conversion

The `.lower()` method converts a string to lowercase, `.upper()` converts it to uppercase, and `.capitalize()` converts the first character to uppercase and the rest to lowercase.

my_string = "Python is FUN"

# Convert to lowercase
lower_case = my_string.lower()
print(f"Lowercase: {lower_case}")

# Convert to uppercase
upper_case = my_string.upper()
print(f"Uppercase: {upper_case}")

#Capitalize string
capitalize_case = my_string.capitalize()
print(f"Capitalize: {capitalize_case}")

Searching for Substrings

The `in` operator checks if a substring exists within a string. The `.find()` method returns the index of the first occurrence of a substring, or -1 if not found. The `.count()` method returns the number of times a substring appears in a string.

my_string = "This is a sample string"

# Check if a substring exists
contains_substring = "sample" in my_string
print(f"Contains 'sample': {contains_substring}")

# Find the index of a substring
index = my_string.find("sample")
print(f"Index of 'sample': {index}")

#Find the index of the last occurence of the substring
last_index = my_string.rfind("is")
print(f"Last Index of 'is': {last_index}")


# Count the occurrences of a substring
count = my_string.count("is")
print(f"Count of 'is': {count}")

Splitting and Joining Strings

The `.split()` method splits a string into a list of substrings based on a delimiter. The `.join()` method joins a list of strings into a single string, using a specified separator.

my_string = "apple,banana,orange"

# Split the string into a list of substrings
split_list = my_string.split(",")
print(f"Split list: {split_list}")

# Join a list of strings into a single string
joined_string = "-".join(split_list)
print(f"Joined string: {joined_string}")

String Stripping

The `.strip()` method removes leading and trailing whitespace from a string. The `.lstrip()` method removes only leading whitespace, and `.rstrip()` removes only trailing whitespace.

my_string = "   leading and trailing spaces   "

# Remove leading and trailing whitespace
stripped_string = my_string.strip()
print(f"Stripped string: '{stripped_string}'")

# Remove leading whitespace
lstrip_string = my_string.lstrip()
print(f"Left stripped string: '{lstrip_string}'")

# Remove trailing whitespace
rstrip_string = my_string.rstrip()
print(f"Right stripped string: '{rstrip_string}'")

Real-Life Use Case Section

These methods are crucial in data cleaning and processing. For instance, normalizing user input (converting to lowercase for case-insensitive comparisons), parsing CSV files (splitting lines by commas), and removing extra spaces from text are common tasks.

# Normalizing user input
user_input = "  UserNAme  "
normalized_input = user_input.strip().lower()
print(f"Normalized input: {normalized_input}")

Best Practices

  • Choose the appropriate string method based on the specific task.
  • Understand the behavior of each method, especially regarding edge cases (e.g., `.find()` returning -1 when a substring is not found).
  • Chain methods together for concise code (e.g., `.strip().lower()`).

Interview Tip

Be familiar with these common string methods and their usage. Be able to explain the difference between `.find()` and `in`, and the importance of string stripping when dealing with user input.

When to use them

  • Case Conversion: Standardizing text for comparisons.
  • Searching: Finding specific patterns or substrings.
  • Splitting: Breaking down strings into smaller parts.
  • Stripping: Cleaning up whitespace.

Memory footprint

Each of these methods creates a new string object. Be mindful of this when working with very large strings or in performance-critical sections of code.

Alternatives

  • Regular expressions can offer more powerful and flexible searching and manipulation capabilities.

Pros

  • These methods are built-in and readily available.
  • They are generally efficient for common string operations.
  • They make code more readable and maintainable.

Cons

  • They can create new string objects, impacting memory usage.
  • Regular expressions may be required for more complex tasks.

FAQ

  • What happens if I call `.split()` without a delimiter?

    If you call `.split()` without a delimiter, it splits the string by whitespace (spaces, tabs, newlines).
  • How can I check if a string starts or ends with a specific substring?

    Use the `.startswith()` and `.endswith()` methods: `my_string.startswith("Hello")` and `my_string.endswith("World")`.