String Manipulation
In this chapter, you will learn about string operations and methods, regular expressions, and string formatting.
String Operations and Methods
Strings in Python are a sequence of characters, and there are many operations and methods that can be applied to them. Some of the most commonly used ones include:
len(): returns the length of the string.+: concatenates two strings.*: repeats a string a certain number of times.in/not in: checks if a substring is present or not..lower(): converts a string to lowercase..upper(): converts a string to uppercase..strip(): removes whitespace from the beginning and end of a string..replace(): replaces a substring with another.
Here’s an example that demonstrates some of these operations:
Regular Expressions
Regular expressions are a powerful tool for working with strings. They are a way to match patterns in text using special syntax. The re module in Python provides support for regular expressions.
Here’s an example that demonstrates how to use regular expressions to match a pattern:
String Formatting
String formatting allows you to create formatted strings by substituting values into a string. There are several ways to format strings in Python, including using the % operator, the format() method, and f-strings.
Here’s an example that demonstrates how to use f-strings:
This will output: My name is Alice and I am 25 years old.
