How To Modify String In Python?

This article will teach Python modify strings with examples, hopefully fulfilling education needs.

In Python, strings can be manipulated using built-in methods.

Python Modify Strings In Upper Case

Strings returned by upper() are always capitalized:

Example

mx = "Nano tech" print(mx.upper())


Remove Whitespace

When you modify Python strings, you often want to eliminate whitespace before and/or after the string text.

Using strip(), any whitespace at the start or end of a string is removed:

Example

mx = " Nano Tech " print(mx.strip()) # It will return "Nano Tech"

Edit Strings In Lower Case

A lower() method returns a string that is in lower case:

Example

mx = "NANO TECH" print(mx.lower())

Split String

A list of items is returned by the split() method when the text between a separator is specified.

If the separator appears in the string, the split() method splits it into substrings:

Example

mrx = "Elon, Reeve, Musk" print(mrx.split(",")) #Output will be ['Elon', ' Reeve', 'Musk']

Python Replace Character In String

Python’s replace() function returns a string containing a new substring for every occurrence of the original string.

replace() Syntax:

syntax: string.replace(old, new, count)

Parameters: 

  • old – Replaces the old substring.
  • new – a new substring to replace the old one.
  • count – (Optional ) Count of times to replace the old substring with the new substring.
  • Return Value : The string will be replaced with a new substring where all occurrences of the original string will be replaced.

In the example below, we will replace the character “e” with “a“.

Example replace all character in string:

mrx = "Ben, Ten!" print(mrx.replace("e", "a"))

Replace Multiple Character or Words In A String:

In the example below, we will replace the word “virtual” with the word “physical“.

Example:

mrx = "In a virtual environment, the user can experience realistic images, sounds, and other sensations by using either virtual reality headsets or multi-projected environments." print(mrx.replace("virtual", "physical"))

Replace numbers of Character or Words In A String:

In the examples below, we will replace character “i” with “m” 2 times in a string.

Example: 

mrx = "iphone, ios and android" print(mrx.replace("i", "m", 2))

String Modifiers advantages

Here are some advantages of using string modifiers in Python:

  1. String modifiers allow you to manipulate and transform strings in various ways. For example, you can convert the case of a string using upper() or lower(), capitalize the first letter of each word using title(), or swap the case of letters using swapcase(). These modifiers make it easy to modify strings according to specific formatting or transformation requirements.
  2. Python provides the + operator for string concatenation, allowing you to join multiple strings together. This is useful for constructing complex strings by combining smaller string components. String concatenation is particularly advantageous when working with dynamic or variable data, as it enables the creation of customized strings based on specific conditions or inputs.
  3. String modifiers offer convenient ways to format strings with dynamic values. The format() method or f-strings (formatted string literals) allow you to insert variables or expressions into strings, resulting in dynamic output. This is especially useful when generating output with variable or calculated values, such as creating dynamic error messages or generating reports.
  4. String modifiers provide methods for searching for specific substrings within a string and replacing them with new values. For example, replace() allows you to find and replace occurrences of a substring with another substring. This can be helpful for data cleansing, modifying text patterns, or performing global substitutions.
  5. Python offers methods to split strings into substrings and join them back together. split() splits a string based on a specified delimiter, creating a list of substrings. Conversely, join() joins a list of strings into a single string, using a specified separator. These modifiers are useful for parsing input, handling file formats, or working with structured textual data.
  6. String modifiers enable you to remove whitespace or specific characters from the beginning or end of a string using strip(), lstrip(), or rstrip(). These methods are handy for cleaning up user input, processing file data, or handling strings with leading/trailing whitespace.
  7. String modifiers allow you to validate and manipulate strings based on certain criteria. For instance, isalpha() checks if a string consists of alphabetic characters only, while isdigit() verifies if a string represents a numeric value. These modifiers can be used to perform data validation, input sanitization, or filtering operations on strings.

You have now concluded learning of Python modify strings – For lists we have a Python Lists chapter that will give you more details about Lists.

Show your appreciation or provide constructive criticism by leaving a reaction below.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *