Tutorial Playlist
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.
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.
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!
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!"
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:
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:
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:
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:
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.
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:
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:
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.
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.
PAVAN VADAPALLI
popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
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. .