Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

Python string updating, replacing and deleting

In Python, strings are immutable, meaning you cannot directly update, replace, or delete individual characters within a string. However, you can create a new string with the desired modifications using string slicing, concatenation, or built-in string methods like replace().

  • Updating a string (example: change the character at index 4):

Since strings are immutable, you cannot directly update a character in the string. You can create a new string with the desired character using string slicing and concatenation.

original_string = "Hello, World!"
index = 4
new_character = "X"

# Using slicing and concatenation
updated_string = original_string[:index] + new_character + original_string[index + 1:]
print(updated_string)  # Output: HellX, World!
  • Replacing a substring:

To replace a substring in a string, you can use the replace() method. This method takes two arguments: the old substring and the new substring. It returns a new string with all occurrences of the old substring replaced with the new substring.

original_string = "Hello, World! Have a nice World!"

# Replace all occurrences of 'World' with 'Earth'
replaced_string = original_string.replace("World", "Earth")
print(replaced_string)  # Output: Hello, Earth! Have a nice Earth!

To limit the number of replacements, you can pass an optional third argument, count. If count is provided, only the first count occurrences will be replaced.

original_string = "Hello, World! Have a nice World!"

# Replace only the first occurrence of 'World' with 'Earth'
replaced_string = original_string.replace("World", "Earth", 1)
print(replaced_string)  # Output: Hello, Earth! Have a nice World!
  • Deleting a substring:

Since strings are immutable, you cannot directly delete a substring from a string. However, you can create a new string without the substring using string slicing, concatenation, or the replace() method.

original_string = "Hello, World!"

# Delete the substring ', World' using the replace() method
deleted_string = original_string.replace(", World", "")
print(deleted_string)  # Output: Hello

Keep in mind that these operations all create new strings rather than modifying the original strings. If you need to frequently modify strings, you may want to consider using mutable data structures like lists or the bytearray type.

  1. Replacing characters in Python strings:

    • Description: Replace specific characters or substrings in a string.
    • Example Code:
      original_string = "Hello World"
      modified_string = original_string.replace('l', 'z')
      
  2. Updating substring in Python string:

    • Description: Update a specific substring within a string.
    • Example Code:
      original_string = "Hello World"
      substring_to_replace = "World"
      replacement_substring = "Python"
      modified_string = original_string.replace(substring_to_replace, replacement_substring)
      
  3. Deleting characters from Python strings:

    • Description: Remove specific characters or substrings from a string.
    • Example Code:
      original_string = "Hello World"
      characters_to_remove = "l"
      modified_string = original_string.replace(characters_to_remove, '')
      
  4. String modification with replace() method in Python:

    • Description: The replace() method is commonly used for string modification by replacing specified substrings.
    • Example Code:
      original_string = "Hello World"
      modified_string = original_string.replace('Hello', 'Hi')
      
  5. Slicing and updating portions of a Python string:

    • Description: Use slicing to extract and update portions of a string.
    • Example Code:
      original_string = "Hello World"
      start_index = 6
      end_index = 11
      updated_portion = "Python"
      modified_string = original_string[:start_index] + updated_portion + original_string[end_index:]
      
  6. Remove specific characters from Python strings:

    • Description: Remove specified characters using various methods, like replace() or list comprehension.
    • Example Code:
      original_string = "Hello World"
      characters_to_remove = "lo"
      modified_string = ''.join(char for char in original_string if char not in characters_to_remove)
      
  7. Updating multiple occurrences in Python strings:

    • Description: Replace or update multiple occurrences of a substring in a string.
    • Example Code:
      original_string = "Hello World, World"
      substring_to_replace = "World"
      replacement_substring = "Python"
      modified_string = original_string.replace(substring_to_replace, replacement_substring, 1)
      
  8. Deleting substrings with string manipulation in Python:

    • Description: Delete specific substrings from a string using methods like replace().
    • Example Code:
      original_string = "Hello World, World"
      substring_to_delete = "World"
      modified_string = original_string.replace(substring_to_delete, '')