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
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()
.
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!
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!
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.
Replacing characters in Python strings:
original_string = "Hello World" modified_string = original_string.replace('l', 'z')
Updating substring in Python string:
original_string = "Hello World" substring_to_replace = "World" replacement_substring = "Python" modified_string = original_string.replace(substring_to_replace, replacement_substring)
Deleting characters from Python strings:
original_string = "Hello World" characters_to_remove = "l" modified_string = original_string.replace(characters_to_remove, '')
String modification with replace() method in Python:
replace()
method is commonly used for string modification by replacing specified substrings.original_string = "Hello World" modified_string = original_string.replace('Hello', 'Hi')
Slicing and updating portions of a Python string:
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:]
Remove specific characters from Python strings:
replace()
or list comprehension.original_string = "Hello World" characters_to_remove = "lo" modified_string = ''.join(char for char in original_string if char not in characters_to_remove)
Updating multiple occurrences in Python strings:
original_string = "Hello World, World" substring_to_replace = "World" replacement_substring = "Python" modified_string = original_string.replace(substring_to_replace, replacement_substring, 1)
Deleting substrings with string manipulation in Python:
replace()
.original_string = "Hello World, World" substring_to_delete = "World" modified_string = original_string.replace(substring_to_delete, '')