replace(), join(), split(), rsplit(), and splitlines() methods in Python

UpComing SoftWare Training Demos

๐Ÿš€ *Gen AI Engineerย Telugu (Production Focused)*
๐Ÿ—“๏ธ *Date:* 30th Sept 2026, 07:00AM IST
๐Ÿ“ *Register Now!:*
https://www.vlrt.in/gr
๐Ÿ‘ฅ *Join WA Community:*
https://www.vlrt.in/gw
๐Ÿ’ก*Course Content:*
https://www.vlrt.in/ai
โ–ถ๏ธ *Demo Videos:*
https://www.vlrt.in/gv

๐Ÿš€ *Service now Admin/ Development (ITSM)FREE Demo in Telugu*
๐Ÿ—“๏ธ *Date:* 30th Sept 2026, 08:00AM IST
๐Ÿ“ *Register Now!:*
https://www.vlrt.in/7r
๐Ÿ‘ฅ *Join WA Community:*
https://www.vlrt.in/7w
๐Ÿ’ก*Course Content:*
https://www.vlrt.in/7c
โ–ถ๏ธ *Demo Videos:*
https://www.vlrt.in/7v

๐Ÿš€ *Vulnerability Management Training Demo*
๐Ÿ—“๏ธ *Date:* 30th Sept 2026, 09:00 AM IST
๐Ÿ“*Register Now!:*
https://www.vlrt.in/vr
๐Ÿ‘ฅ *Join Community:*
https://www.vlrt.in/cw
๐Ÿ’ก *Course Content:*
https://www.vlrt.in/vc
โ–ถ๏ธ *Demo Videos:*
https://www.vlrt.in/Vm

1. replace() Method

Purpose: Replaces occurrences of a substring with another substring.
Syntax:

python

string.replace(old, new[, count])
  • old: Substring to replace.
  • new: Substring to replace with.
  • countย (optional): Maximum number of replacements (default: all).

Examples:

Example 1: Basic Replacement

python

text = "Hello World"
new_text = text.replace("World", "Python")
print(new_text)  # Output: "Hello Python"

Example 2: Limiting Replacements (count)

python

text = "apple apple apple"
new_text = text.replace("apple", "orange", 2)
print(new_text)  # Output: "orange orange apple"

2. join() Method

Purpose: Joins an iterable (list, tuple, etc.) of strings into a single string using a separator.
Syntax:

python

separator.join(iterable)
  • iterable: Must contain strings (e.g.,ย list,ย tuple).

Examples:

Example 1: Joining a List with a Space

python

words = ["Python", "is", "awesome"]
sentence = " ".join(words)
print(sentence)  # Output: "Python is awesome"

Example 2: Joining with a Comma

python

fruits = ["apple", "banana", "cherry"]
csv = ",".join(fruits)
print(csv)  # Output: "apple,banana,cherry"

3. split() Method

Purpose: Splits a string into a list based on a delimiter.
Syntax:

python

string.split(sep=None, maxsplit=-1)
  • sep: Delimiter (default: whitespace).
  • maxsplit: Maximum splits (default: all).

Examples:

Example 1: Splitting by Comma

python

text = "apple,banana,cherry"
fruits = text.split(",")
print(fruits)  # Output: ['apple', 'banana', 'cherry']

Example 2: Limiting Splits (maxsplit)

python

text = "one two three four"
parts = text.split(" ", 2)
print(parts)  # Output: ['one', 'two', 'three four']

4. rsplit() Method

Purpose: Similar to split(), but splits from the right (end of string).
Syntax:

python

string.rsplit(sep=None, maxsplit=-1)
  • sep: Delimiter (default: whitespace).
  • maxsplit: Maximum splits (default: all).

Examples:

Example 1: Basic rsplit()

python

text = "apple,banana,cherry"
fruits = text.rsplit(",")
print(fruits)  # Output: ['apple', 'banana', 'cherry'] (same as split)

Example 2: Limiting Splits from the Right

python

text = "one two three four"
parts = text.rsplit(" ", 2)
print(parts)  # Output: ['one two', 'three', 'four']

5. splitlines() Method

Purpose: Splits a string at line breaks (\n, \r\n, etc.).
Syntax:

python

string.splitlines([keepends=False])
  • keepends: Ifย True, keeps line breaks (default:ย False).

Examples:

Example 1: Basic splitlines()

python

text = "Line 1\nLine 2\r\nLine 3"
lines = text.splitlines()
print(lines)  # Output: ['Line 1', 'Line 2', 'Line 3']

Example 2: Keeping Line Breaks (keepends=True)

python

text = "Hello\nWorld"
lines = text.splitlines(keepends=True)
print(lines)  # Output: ['Hello\n', 'World']

Summary Table

MethodPurposeExample
replace()Replace substrings"abc".replace("a", "x") โ†’ "xbc"
join()Combine strings with a separator",".join(["a", "b"]) โ†’ "a,b"
split()Split string by delimiter (left-to-right)"a,b,c".split(",") โ†’ ["a", "b", "c"]
rsplit()Split string by delimiter (right-to-left)"a b c".rsplit(" ", 1) โ†’ ["a b", "c"]
splitlines()Split by line breaks"a\nb".splitlines() โ†’ ["a", "b"]

These methods are essential for string manipulation in Python. Let me know if you need further clarification! ๐Ÿš€

Similar Posts

  • Escape Sequences in Python

    Escape Sequences in Python Escape sequences are special character combinations that represent other characters or actions in strings. Here’s a complete list of Python escape sequences with two examples for each: 1. \\ – Backslash python print(“This is a backslash: \\”) # Output: This is a backslash: \ print(“Path: C:\\Users\\Name”) # Output: Path: C:\Users\Name 2. \’ – Single quote…

  • Mutable vs. Immutable Objects in Python ๐Ÿ”„๐Ÿ”’

    Mutable vs. Immutable Objects in Python ๐Ÿ”„๐Ÿ”’ In Python, mutability determines whether an object’s value can be changed after creation. This is crucial for understanding how variables behave. ๐Ÿค” Immutable Objects ๐Ÿ”’ Example 1: Strings (Immutable) ๐Ÿ’ฌ Python Example 2: Tuples (Immutable) ๐Ÿ“ฆ Python Mutable Objects ๐Ÿ“ Example 1: Lists (Mutable) ๐Ÿ“‹ Python Example 2:…

  • Method overriding

    Method overriding is a key feature of object-oriented programming (OOP) and inheritance. It allows a subclass (child class) to provide its own specific implementation of a method that is already defined in its superclass (parent class). When a method is called on an object of the child class, the child’s version of the method is…

  • re.split()

    Python re.split() Method Explained The re.split() method splits a string by the occurrences of a pattern. It’s like the built-in str.split() but much more powerful because you can use regex patterns. Syntax python re.split(pattern, string, maxsplit=0, flags=0) Example 1: Splitting by Multiple Delimiters python import retext1=”The re.split() method splits a string by the occurrences of a pattern. It’s like…

  • Generalization vs. Specialization

    Object-Oriented Programming: Generalization vs. Specialization Introduction Inheritance in OOP serves two primary purposes: Let’s explore these concepts with clear examples. 1. Specialization (Extending Functionality) Specialization involves creating a new class that inherits all features from a parent class and then adds new, specific features. The core idea is reusabilityโ€”you build upon what already exists. Key Principle: Child Class =…

  • ย index(),ย count(),ย reverse(),ย sort()

    Python List Methods: index(), count(), reverse(), sort() Let’s explore these essential list methods with multiple examples for each. 1. index() Method Returns the index of the first occurrence of a value. Examples: python # Example 1: Basic usage fruits = [‘apple’, ‘banana’, ‘cherry’, ‘banana’] print(fruits.index(‘banana’)) # Output: 1 # Example 2: With start parameter print(fruits.index(‘banana’, 2)) # Output: 3 (starts searching…

Leave a Reply

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