Strings in python: Methods, Operations, and Reversing

Strings in Python: Methods, Operations, and Reversing

Strings in Python: Methods, Operations, and Reversing

In this article, we will learn about strings in Python, including how to create them, perform operations, use built-in methods, and reverse a string. Let’s dive in!

1. What is a String in Python?

A string in Python is a sequence of characters enclosed in quotes (' ' or " "). Strings can contain letters, numbers, spaces, and special characters.

2. Creating Strings

You can create strings by using either single or double quotes:

str1 = 'Hello'
str2 = "World"
str3 = '''This is a
multi-line string'''

3. Common String Operations

You can perform several operations on strings, such as concatenation, repetition, and slicing:

  • Concatenation: Joining two strings using the + operator.
  • str1 = "Hello"
    str2 = "World"
    result = str1 + " " + str2  # "Hello World"
  • Repetition: Repeating a string using the * operator.
  • str1 = "Hi "
    result = str1 * 3  # "Hi Hi Hi "
  • Indexing: Access individual characters using an index.
  • str1 = "Python"
    print(str1[0])  # 'P'
  • Slicing: Extract a portion of a string using slicing.
  • str1 = "Python"
    print(str1[0:3])  # 'Pyt'

4. String Methods

Python provides many built-in string methods. Here are some common ones:

  • upper(): Convert a string to uppercase.
  • str1 = "hello"
    print(str1.upper())  # "HELLO"
  • lower(): Convert a string to lowercase.
  • str1 = "HELLO"
    print(str1.lower())  # "hello"
  • strip(): Remove leading and trailing spaces.
  • str1 = "   Hello   "
    print(str1.strip())  # "Hello"
  • replace(): Replace all occurrences of a substring.
  • str1 = "apple"
    print(str1.replace('a', 'o'))  # "opple"
  • split(): Split a string into a list using a delimiter.
  • str1 = "apple,banana,orange"
    print(str1.split(','))  # ['apple', 'banana', 'orange']

5. String Formatting

You can format strings using placeholders or f-strings:

name = "John"
age = 25
sentence = f"My name is {name} and I am {age} years old."
print(sentence)  # "My name is John and I am 25 years old."

6. Reversing a String in Python

Now let's learn how to reverse a string in Python. Here are a few methods:

Method 1: Using Slicing

This is the most common and simplest way to reverse a string:

str1 = "Hello"
reversed_str = str1[::-1]
print(reversed_str)  # "olleH"

Method 2: Using a Loop

Reverse a string using a loop:

str1 = "Hello"
reversed_str = ""
for char in str1:
    reversed_str = char + reversed_str
print(reversed_str)  # "olleH"

Method 3: Using reversed() and join()

You can use reversed() and join() to reverse a string:

str1 = "Hello"
reversed_str = ''.join(reversed(str1))
print(reversed_str)  # "olleH"

Method 4: Using reduce() from functools

from functools import reduce
str1 = "Hello"
reversed_str = reduce(lambda x, y: y + x, str1)
print(reversed_str)  # "olleH"

Published by Nikhil | © 2024 All Rights Reserved

Comments

Popular posts from this blog

Decorators in Pythons | easy way to learn about decorators

Useful Git Commands for Windows