Contents

Mastering Python Strings

Unleash the Power of Strings in Python for Effective Data Manipulation

Website Visitors:

Introduction

Python, a powerful and versatile programming language, provides developers with a rich set of tools to work with strings. Strings are sequences of characters, such as letters, numbers, and symbols, enclosed within quotes. Understanding the intricacies of Python strings is essential for effective data manipulation, text processing, and formatting. In this article, we will delve into the world of Python strings, exploring various string operations, methods, and formatting techniques, accompanied by detailed examples.

Declaring Strings

var = “first_variable” - Can use double quotes

var= ‘first_variable’ - Can use single quotes

var = “” - can be an empty string.

“Value” * 3 - outputs as ValueValueValue

var = ‘first_variable" - Doesn’t work as both single quotes and double quotes are used in the string. You have to end the string with the same quote that you’ve used at the begining.

Basic String Operations

Python offers several basic operations to manipulate strings efficiently:

a. Concatenation: Combining strings using the ‘+’ operator. Example:

1
2
3
4
5
6
7
8
name = "John"
greeting = "Hello, " + name
print(greeting)  # Output: Hello, John

greet = "Hello",name
print(greet) # Output: 'Hello', 'John'

print("Hello",name) # Output: Hello John

b. String Length: Obtaining the length of a string using the len() function. Example:

1
2
message = "Welcome to Python"
print(len(message))  # Output: 18

c. Indexing and Slicing: Accessing specific characters or substrings within a string. Example:

1
2
3
4
text = "Python is amazing"
print(text[0])       # Output: P
print(text[7:9])     # Output: is
print(text[-3:])     # Output: ing

String Methods

Python provides a wide range of built-in methods to perform various operations on strings. Here are a few commonly used string methods:

a. lower() and upper(): Converts a string to lowercase or uppercase, respectively. Example:

1
2
3
text = "Hello, World!"
print(text.lower())   # Output: hello, world!
print(text.upper())   # Output: HELLO, WORLD!

b. split(): Splits a string into a list of substrings based on a delimiter. Example:

1
2
3
sentence = "Python is awesome"
words = sentence.split()
print(words)   # Output: ['Python', 'is', 'awesome']

c. replace(): Replaces occurrences of a substring with another substring. Example:

1
2
3
quote = "Be the change you wish to see in the world"
updated_quote = quote.replace("change", "difference")
print(updated_quote)   # Output: Be the difference you wish to see in the world

String Formatting

Python provides powerful string formatting options to create formatted output. Here are two popular approaches:

a. String Interpolation: Using the format() method to insert values into a string. Example:

1
2
3
4
name = "Alice"
age = 25
message = "My name is {} and I am {} years old.".format(name, age)
print(message)   # Output: My name is Alice and I am 25 years old.

b. F-Strings (Formatted String Literals): Introduced in Python 3.6, F-Strings offer a concise and readable way to format strings. Example:

1
2
3
4
name = "Bob"
age = 30
message = f"My name is {name} and I am {age} years old."
print(message)   # Output: My name is Bob and I am 30 years old.

String formatting in Python allows you to customize the way strings are displayed by using expressions inside curly brackets. These expressions provide control over formatting options. For instance, by using the expression {:.2f}, you can format a number as a floating-point value with two decimal places. The colon serves as a separator if a field name is specified. Additionally, you can align text using the greater than operator (>). For instance, the expression {:>3.2f} would right-align the text by three spaces and format a floating-point number with two decimal places. String formatting is a useful tool for generating clear and easily readable output.

String indexing

String indexing allows you to access individual characters within a string by their position or index. In Python, string indexing starts from 0, where the first character is at index 0, the second character is at index 1, and so on. You can also use negative indexing, where -1 refers to the last character, -2 refers to the second-to-last character, and so forth.

Let’s explore string indexing with some examples:

Example: Accessing Individual Characters

1
2
3
4
text = "Hello, Python"
print(text[0])     # Output: H
print(text[7])     # Output: P
print(text[-1])    # Output: n

In the above example, we access the first character of the string using text[0], which returns “H”. Similarly, text[7] returns the character “P”, and text[-1] returns the last character “n”.

Slicing Substrings

String slicing allows you to extract a portion or substring from a string by specifying a range of indices. First value is the starting position, second value is where do you want the slicing to be done. Spaces are also counted in.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
text = "Hello, Python"
print(text[0:5])    # Output: Hello
print(text[7:])     # Output: Python
print(text[:5])     # Output: Hello - first 5 characters are considered.
print(text[5:])     # Output: , Python - starting from the number,
# all remaining characters are considered.
print(text[-6:-1])  # Output: Pytho
print(text[0:13])   # Output: Hello, Python
print(text[0:])     # Output: Hello, Python
print(text[:])      # Output: Hello, Python

In the above example, text[0:5] returns the substring “Hello”, which includes characters from index 0 up to index 5 (exclusive). Similarly, text[7:] returns the substring starting from index 7 till the end of the string. text[:5] returns the substring from the beginning of the string up to index 5 (exclusive). Lastly, text[-6:-1] returns the substring starting from the sixth-to-last character up to the second-to-last character.

Example: Step in Slicing You can specify a step value while slicing a string to extract every nth character.

1
2
3
4
text = "Hello, Python"
print(text[1:8:2])    # Output: el,P
print(text[::3])      # Output: HlPh
print(text[::-1])     # Output: nohtyP ,olleH

In the above example, text[1:8:2] returns a substring starting from index 1 up to index 8 (exclusive), with a step of 2. It returns every second character, resulting in “el,P”. text[::3] returns a substring with a step of 3, resulting in “HlPh”. Lastly, text[::-1] returns a reversed string.

You can get the index number of the value without counting the characters in sting as shown below:

1
2
3
4
5
"System".index("e") # Output: 4 - start counting from 0 to S, 1 to y,
# 2 to s and so on..
"System".index("s") # Output: 0
"System".index("t") # Output: 3
"System".index("y") # Output: 1

Now that we know the index numbers, we can use it in slicing.

Changing value of strings

Strings are immutable in python which means they cannot be modified directly. For example, lets assume that you want to change the value of k to l in the string as shown below.

1
message = "This is kong string"

If you use message[9] = l, it throws an error as string cannot be changed. Instead you can pick the characters before and after the value you want to change and use the new character you want to use, as shown below.

1
2
message = "This is kong string"
print(message[:8] + "l" + message[9:])

It picks the first 8 characters including space to the left, inserts the letter l, and uses the next 9 characters from the rest of the string.

If we try to locate a substring that doesn’t exist in the string, we’ll receive a ValueError explaining that the substring was not found. We can avoid a ValueError by first checking if the substring exists in the string. This can be done using the in keyword.

1
2
3
animals = "lions tigers and bears"
"horses" in animal
"tigers" in animal

Strip strings

In Python, the strip() method is used to remove leading and trailing whitespace characters from a string. The strip() method returns a new string with the whitespace characters removed.

Here’s an example of how to use the strip() method:

1
2
3
text = "   Hello, World!   "
stripped_text = text.strip()
print(stripped_text)  # Output: "Hello, World!"

In the example above, the strip() method is called on the text string, which contains leading and trailing whitespace characters. The resulting stripped_text variable contains the modified string with the whitespace characters removed.

The strip() method can also be used to remove specific characters or a substring from the beginning and end of a string by passing them as an argument. For example:

1
2
3
text = "+++Hello, World!---"
stripped_text = text.strip("+-")
print(stripped_text)  # Output: "Hello, World!"

In this case, the strip() method removes the plus and minus characters (’+’ and ‘-’) from the beginning and end of the string.

It’s important to note that the strip() method only removes leading and trailing characters, not characters within the string itself. If you want to remove specific characters within the string, you can use other methods like replace(), regular expressions, or string slicing.

Along with the normal strip() method, we also have lstrip() and rstrip() methods using which will remove the spaces on the left or right accordingly.

1
2
test = "How much wood would a woodchuck chuck"
print(test.count("wood"))               # prints 2

You can join strings using the join method as shown below:

1
2
3
4
" ".join(["This", "is", "a", "phrase", "joined", "by", "spaces"])
# Output This is a phrase joined by spaces
"...".join(["This", "is", "a", "phrase", "joined", "by", "triple", "dots"])
# Output This...is...a...phrase...joined...by...triple...dots

Formatting strings

In Python, the format() method is used to format strings by replacing placeholders with corresponding values. It allows you to create dynamic strings by inserting values into specific locations within a string.

Here’s an overview of how to use the format() method with examples:

  1. Positional Arguments:

    1
    2
    3
    4
    
    name = "Alice"
    age = 30
    message = "My name is {} and I'm {} years old.".format(name, age)
    print(message)  # Output: "My name is Alice and I'm 30 years old."
    

    In this example, the curly braces {} are used as placeholders in the message string. The format() method replaces the placeholders with the values provided in the order specified.

  2. Indexed Arguments:

    1
    2
    3
    4
    
    name = "Bob"
    age = 25
    message = "My name is {0} and I'm {1} years old. {0} is my first name.".format(name, age)
    print(message)  # Output: "My name is Bob and I'm 25 years old. Bob is my first name."
    

    Here, the curly braces {} include index numbers to specify the order of the arguments to be inserted. In this case, {0} corresponds to name and {1} corresponds to age.

  3. Named Arguments:

    1
    2
    3
    
    person = {"name": "Charlie", "age": 35}
    message = "My name is {name} and I'm {age} years old.".format(**person)
    print(message)  # Output: "My name is Charlie and I'm 35 years old."
    

    In this example, a dictionary person is used to store the values. The format() method takes named arguments by unpacking the dictionary using **.

  4. Accessing Object Attributes:

    1
    2
    3
    4
    5
    6
    7
    8
    
    class Person:
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
    person = Person("David", 40)
    message = "My name is {person.name} and I'm {person.age} years old.".format(person=person)
    print(message)  # Output: "My name is David and I'm 40 years old."
    

    Here, the format() method can access attributes of an object directly by using dot notation within the placeholders.

  5. Formatting Specifiers:

    1
    2
    3
    
    pi = 3.14159
    formatted_pi = "The value of pi is approximately {:.2f}".format(pi)
    print(formatted_pi)  # Output: "The value of pi is approximately 3.14"
    

    In this example, :.2f is used within the placeholder to format the pi value as a floating-point number with two decimal places.

  6. Aligning the output

    Below code specifies the output to left should be 3 characters and print a character called F and |. To the right, it should print 6 characters max with 2 decimal places and print a letter C.

    {:>3.2f} would align the text three spaces to the right, as well as specify a float number with two decimal places.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    def to_celsius(x):
      return (x-32)*5/9
    
    for x in range(0,101,10):
      print("{:>3} F | {:>6.2f} C".format(x, to_celsius(x)))
    
    # Output:
      0 F | -17.78 C
     10 F | -12.22 C
     20 F |  -6.67 C
     30 F |  -1.11 C
     40 F |   4.44 C
     50 F |  10.00 C
     60 F |  15.56 C
     70 F |  21.11 C
     80 F |  26.67 C
     90 F |  32.22 C
    100 F |  37.78 C
    

Formatting expressions

Expr Meaning Example
{:d} integer value “{0:.0f}".format(10.5) → ‘10’
{:.2f} floating point with that many decimals ‘{:.2f}’.format(0.5) → ‘0.50’
{:.2s} string with that many characters ‘{:.2s}’.format(‘Python’) → ‘Py’
{:<6s} string aligned to the left that many spaces ‘{:<6s}’.format(‘Py’) → ‘Py    '
{:>6s} string aligned to the right that many spaces ‘{:>6s}’.format(‘Py’) → ’    Py’
{:^6s} string centered in that many spaces ‘{:^6s}’.format(‘Py’) → ’  Py  '

Conclusion

In conclusion, lists, list comprehension, and tuples are essential data structures in Python. Lists offer flexibility and mutability, allowing for dynamic modifications, while list comprehension provides a concise way to create new lists. Tuples, with their immutability, ensure data integrity and stability. Understanding and mastering these concepts will empower you to efficiently store, manipulate, and process collections of data in Python.

Your inbox needs more DevOps articles.

Subscribe to get our latest content by email.