View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
  1. Home
python

Python Tutorials - Elevate Your Coding Skills with Comprehensive Tutorials

Discover Python tutorials covering everything from the basics to advanced topics. Whether you're new to programming or looking to level up your skills, our tutorials will guide you every step of the way

  • 201 Lessons
  • 33 Hours
right-top-arrow

Tutorial Playlist

200 Lessons
24

String Methods Python

Updated on 11/03/20245,700 Views

Introduction

In this tutorial, we will delve deep into various String Methods Python. Python provides a built-in class, str, which comes with several methods to manipulate and analyze strings. For professionals looking to upskill, understanding these methods is crucial for text processing, data cleaning, and other common tasks. Additionally, mastering these methods will enable you to handle textual data efficiently and lay a solid foundation for advanced topics like regular expressions and natural language processing.

Overview

String methods Python encompass a variety of functions. From modifying the case of the string, stripping whitespace, checking if the string starts or ends with a particular substring, to finding the index of a substring, and many more. We will explore string methods in Python with examples to provide a comprehensive understanding.

This tutorial is designed for those who already have a basic understanding of Python and want to deepen their knowledge of string manipulation techniques, which are fundamental skills for every Python developer.

Changing the Case of Strings

  • upper() method converts all characters in the string to uppercase.
  • lower() method converts all characters in the string to lowercase.
  • title() method capitalizes the first letter of each word in the string.
  • capitalize() method capitalizes only the first letter of the entire string.

Code:

original_string = "Hello, World!"
# Convert to uppercase
uppercase_string = original_string.upper()
print(uppercase_string)  # Output: HELLO, WORLD!
# Convert to lowercase
lowercase_string = original_string.lower()
print(lowercase_string)  # Output: hello, world!
# Convert to title case (capitalize the first letter of each word)
titlecase_string = original_string.title()
print(titlecase_string)  # Output: Hello, World!
# Capitalize only the first letter of the string
capitalize_string = original_string.capitalize()
print(capitalize_string)  # Output: Hello, world!

The Essential String Methods in Python

Method Description

str.upper() Converts all characters in the string to uppercase.

str.lower() Converts all characters in the string to lowercase.

str.title() Capitalizes the first letter of each word in the string.

str.capitalize() Capitalizes the first letter of the string.

str.strip() Removes leading and trailing whitespace characters.

str.lstrip() Removes leading whitespace characters.

str.rstrip() Removes trailing whitespace characters.

str.startswith(prefix) Checks if the string starts with the specified prefix.

str.endswith(suffix) Checks if the string ends with the specified suffix.

str.replace(old, new) Replaces occurrences of old with new in the string.

str.find(sub) Returns the index of the first occurrence of sub in the string, or -1 if

not found.

str.index(sub)Similar to find(), but raises an exception if sub is not found.

str.count(sub)Counts the number of non-overlapping occurrences of sub in the

string.

str.split()Splits the string into a list of substrings using whitespace as the

default separator.

str.split(sep)Splits the string into a list of substrings using sep as the separator.

str.join(iterable)Joins the elements of an iterable (e.g., a list) with the string as the

separator.

str.isdigit()Checks if the string consists only of digits.

str.isalpha()Checks if the string consists only of alphabetic characters.

str.isalnum()Checks if the string consists only of alphanumeric characters.

str.islower()Checks if all characters in the string are lowercase.

str.isupper()Checks if all characters in the string are uppercase.

Example:

Code:

# Original string
text = " Hello, World! "
# Convert to uppercase
uppercase_text = text.upper()
print(uppercase_text)  # Output: "   HELLO, WORLD!   "
# Convert to lowercase
lowercase_text = text.lower()
print(lowercase_text)  # Output: "   hello, world!   "
# Capitalize the first letter of each word
titlecase_text = text.title()
print(titlecase_text)  # Output: "   Hello, World!   "
# Remove leading and trailing spaces
stripped_text = text.strip()
print(stripped_text)   # Output: "Hello, World!"
# Check if the string starts with "Hello"
starts_with_hello = text.startswith("Hello")
print(starts_with_hello)  # Output: False
# Check if the string ends with "World!"
ends_with_world = text.endswith("World!")
print(ends_with_world)    # Output: False
# Replace "Hello" with "Hi"
replaced_text = text.replace("Hello", "Hi")
print(replaced_text)      # Output: "   Hi, World!   "
# Split the string into a list of words
split_words = text.split()
print(split_words)        # Output: ['Hello,', 'World!']
# Join the words in the list with a space as the separator
joined_text = ' '.join(split_words)
print(joined_text)        # Output: "Hello, World!"

Example of The format() method in Python

Code:

# Using format() method for string formatting
name = "Alice"
age = 30
# Basic string formatting
formatted_string = "My name is {} and I am {} years old.".format(name, age)
print(formatted_string)
# Using positional placeholders
formatted_pos = "Name: {0}, Age: {1}".format(name, age)
print(formatted_pos)
# Using named placeholders
formatted_named = "Name: {n}, Age: {a}".format(n=name, a=age)
print(formatted_named)
# Combining positional and named placeholders
combined_formatted = "Name: {}, Age: {a}".format(name, a=age)
print(combined_formatted)
# Formatting with different data types
pi = 3.14159
formatted_float = "The value of pi is {:.2f}".format(pi)
print(formatted_float)
# Formatting using index and value from a tuple
person = ("Bob", 25)
formatted_tuple = "Name: {0[0]}, Age: {0[1]}".format(person)
print(formatted_tuple)

Explanation:

In this example:

  • The curly braces {} are used as placeholders for values that will be inserted using the str.format() method.
  • The placeholders can be positional (indexed) or named. You can mix both types of placeholders.
  • In the formatted_float example, :.2f specifies formatting for a floating-point number with 2 decimal places.
  • The formatted_tuple example shows how to use an index and value from a tuple.
  • Remember that the str.format() method provides flexible and powerful string formatting capabilities. You can use it to create customized and dynamic output by substituting values into the placeholders.

Example of Creating Strings in Python

Code:

# Using single quotes
single_quoted_string = 'Hello, World!'
print(single_quoted_string)
# Using double quotes
double_quoted_string = "Hello, World!"
print(double_quoted_string)
# Using triple quotes for multiline strings
multiline_string = '''This is a
multiline
string.'''
print(multiline_string)
# Escaping special characters
escaped_string = "She said, \"Hello!\""
print(escaped_string)
# Concatenating strings
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name)

Explanation:

In the above example:

  • Single quotes ('...') and double quotes ("...") are used to define strings.
  • Triple quotes ('''...''' or """...""") are used to create multiline strings.
  • You can escape special characters using a backslash (\) before them.
  • You can concatenate strings using the + operator.

Example of Indexing and Splitting in Strings

Code:

# Indexing strings
my_string = "Hello, World!"
# Accessing individual characters using indexing
first_char = my_string[0]
second_char = my_string[7]
last_char = my_string[-1]  # Negative index counts from the end
print("First char:", first_char)
print("Second char:", second_char)
print("Last char:", last_char)
# Slicing strings to get substrings
substring = my_string[7:12]
print("Substring:", substring)  # Output: "World"
# Splitting strings
sentence = "Python is a powerful programming language."
words = sentence.split()  # Split using whitespace by default
print("Words:", words)
# Splitting with a specified delimiter
csv_data = "Alice,30,Engineer"
csv_values = csv_data.split(",")
print("CSV values:", csv_values)

Explanation:

In this code:

  • Indexing is used to access individual characters in the string. Positive indices start from 0, and negative indices count from the end.
  • Slicing is used to extract substrings from the string. The syntax start:end specifies the range of characters to include in the substring.
  • The split() method is used to split strings into a list of substrings. By default, it splits on whitespace.
  • In the CSV example, the split() method is used with a comma as the delimiter to split a CSV string into values.

Example of Reassigning Strings in Python

Code:

# Original string
original_string = "Hello, World!"
# Reassigning a string variable
modified_string = original_string.replace("Hello", "Hi")
print("Original:", original_string)
print("Modified:", modified_string)
# Reassigning with concatenation
greeting = "Hi"
name = "Alice"
full_greeting = greeting + ", " + name + "!"
print("Full Greeting:", full_greeting)
# Updating string using slicing
mutable_string = "mutable"
mutable_list = list(mutable_string)  # Convert to a list
mutable_list[3] = 'a'  # Change the character at index 3
updated_string = ''.join(mutable_list)  # Convert back to string
print("Updated:", updated_string)

Explanation:

In this code:

  • The replace() method is used to create a modified version of the original string by replacing "Hello" with "Hi".
  • Reassigning a variable involves assigning a new string to the same variable name. The old string is discarded.
  • Concatenation is used to create a new string by combining the greeting, name, and other strings.
  • The mutable_string example shows how to update a string by modifying its characters using indexing and slicing.

Deleting a String in Python

Code:

# Original string
original_string = "Hello, World!"
# Deleting characters using slicing
deleted_string = original_string[:7] + original_string[13:]
print("Original:", original_string)
print("Deleted:", deleted_string)

In this example, the deleted_string is created by excluding the characters at indices 7 to 12 (inclusive) from the original_string. The result is a new string with the characters effectively "deleted."

Code:

# Original string
original_string = "Hello, World!"
# Deleting a substring using replace
deleted_substring = original_string.replace("Hello, ", "")
print("Original:", original_string)
print("Deleted Substring:", deleted_substring)

In this case, the replace() method replaces the "Hello, " substring with an empty string, effectively removing it from the original string.

String Operators in Python

Code:

# Concatenation
string1 = "Hello, "
string2 = "World!"
concatenated_string = string1 + string2
print("Concatenated:", concatenated_string)
# Repetition
repeated_string = string1 * 3
print("Repeated:", repeated_string)
# Membership
check_membership = "Hello" in concatenated_string
print("Membership Check:", check_membership)
# String Formatting (using f-strings)
name = "Alice"
age = 30
formatted_string = f"My name is {name} and I am {age} years old."
print("Formatted:", formatted_string)
# Length (using len())
length = len(concatenated_string)
print("Length:", length)

Explanation:

In this example:

  • The + operator is used for string concatenation, combining two strings.
  • The * operator is used for string repetition, repeating a string multiple times.
  • The in keyword is used for membership testing, checking if a substring is present in a string.
  • The f-string format (f"...") is used for string formatting, allowing you to embed variables directly into the string.
  • The len() function is used to determine the length of a string.

Example of String Formatting in Python

Code:

# Using %-formatting (old-style formatting)
name = "Alice"
age = 30
formatted_string = "My name is %s and I am %d years old." % (name, age)
print("Formatted (%%-formatting):", formatted_string)
# Using str.format() method (new-style formatting)
formatted_string = "My name is {} and I am {} years old.".format(name, age)
print("Formatted (str.format()):", formatted_string)
# Using f-strings (formatted string literals)
formatted_string = f"My name is {name} and I am {age} years old."
print("Formatted (f-strings):", formatted_string)
# Formatting floating-point numbers
pi = 3.14159
formatted_float = "The value of pi is {:.2f}".format(pi)
print(formatted_float)
# Formatting with named placeholders
formatted_named = "Name: {n}, Age: {a}".format(n=name, a=age)
print(formatted_named)
# Using positional placeholders with str.format()
formatted_pos = "Name: {0}, Age: {1}".format(name, age)
print(formatted_pos)
# Combining positional and named placeholders
combined_formatted = "Name: {}, Age: {a}".format(name, a=age)
print(combined_formatted)

Explanation:

In these example:

  • %-formatting is the older formatting method that uses % placeholders in the string.
  • str.format() is a newer method where placeholders are enclosed in curly braces {}.
  • f-strings (formatted string literals) are introduced in Python 3.6 and provide a concise way to embed expressions directly in the string.

Conclusion

Understanding string methods is crucial for any professional looking to upskill in Python. These methods form the foundation of text processing and manipulation, which are common tasks in various fields like data science, web development, and automation. upGrad offers a variety of upskilling courses that can help you master Python and other essential skills required in the modern job market.

FAQs

1. What are some commonly used string methods in Python?

Some commonly used string methods in Python include upper, lower, replace, split, and join, among others.

2. What is string slicing in Python?

String slicing in Python refers to selecting a range of characters from a string using their index positions.

3. Are there any similarities between string and list methods in Python?

Yes, there are similarities between string and list methods in Python. Both lists and strings have methods for finding elements, counting elements, and checking if elements exist.

4. Are there any special modules in Python for string manipulation?

Yes, Python has a built-in module called string. This provides additional functions and constants for string manipulation that aren't included in the built-in str class.

5. Are there any alternatives to using string methods in Python?

Python provides regular expressions (regex) as a powerful alternative for advanced string manipulation. However, for basic operations, string methods are more efficient.

Ready to challenge yourself? Take our Free Python Quiz!

image
right-top-arrowright-top-arrow
Need More Help? Talk to an Expert
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. .