Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Master Python String Operations: Replace, Join, Split, Reverse, and Case Conversion

In Python, every entity is an object—including strings. A string is simply a sequence of characters enclosed in quotes.

Example:
var = "Hello World!"

This guide walks you through the most common string operations, backed by real examples and best‑practice tips.

Accessing Characters in Strings

Python does not have a distinct character type; single characters are simply one‑length strings. Use square brackets and indices to slice.

var1 = "Guru99!"
var2 = "Software Testing"
print("var1[0]:", var1[0])
print("var2[1:5]:", var2[1:5])

Output:
var1[0]: G
var2[1:5]: oftw

String Operators

Operators enable quick manipulation:

OperatorDescriptionExample
[]Access a single characterx = "Guru"
print(x[1]) — outputs "u"
[:]Slice a range (end exclusive)x = "Guru"
print(x[1:3]) — outputs "ur"
in / not inCheck membershipprint("u" in x) — True
r/RRaw string literal (escape characters ignored)print(r"\n") — outputs "\\n"
%String formatting (old style)print('%s %d' % ('guru', 99)) — "guru 99"
+Concatenate two stringsprint("Guru" + "99") — "Guru99"
*Repeat stringprint("Guru" * 2) — "GuruGuru"

Updating a String

Because strings are immutable, re‑assign the variable to a new value.

x = "Hello World!"
print(x[:6])          # "Hello"
print(x[0:6] + "Guru99")  # "Hello Guru99"

Replacing Substrings

The replace() method returns a new string with all occurrences of a substring replaced.

old = "I like Guru99"
new = old.replace('like', 'love')
print(new)  # "I love Guru99"

Changing Case

Convert to upper, lower, or capitalize the first character.

text = "python at guru99"
print(text.upper())      # "PYTHON AT GURU99"
print(text.capitalize()) # "Python at guru99"
print(text.upper().lower())  # back to lower case

Joining Characters

The join() method concatenates an iterable of strings using a separator.

print(":".join("Python"))  # "P:y:t:h:o:n"

Reversing a String

Use reversed() and join() to reverse.

s = "12345"
print(''.join(reversed(s)))  # "54321"

Splitting Strings

Break a string into a list based on a delimiter.

sentence = "guru99 career guru99"
print(sentence.split(' '))  # ['guru99', 'career', 'guru99']
print(sentence.split('r'))  # ['gu', 'u99 ca', 'ee', ' gu', 'u99']

Immutability Reminder

Strings cannot be altered in place. Methods like replace() return a new string.

x = "Guru99"
print(x.replace("Guru99", "Python"))  # still "Guru99"

# To change the variable
x = x.replace("Guru99", "Python")
print(x)  # "Python"

Python 2 Compatibility

Below is the equivalent code for Python 2, using print statements without parentheses.

# Accessing values
var1 = "Guru99!"
var2 = "Software Testing"
print "var1[0]:", var1[0]
print "var2[1:5]:", var2[1:5]

# Update example
x = "Hello World!"
print x[:6]
print x[0:6] + "Guru99"

# Replace example
old = 'I like Guru99'
print old.replace('like', 'love')

# Case conversion
text = "python at guru99"
print text.upper()
print text.capitalize()
print text.lower()

# Join
print ":".join("Python")

# Reverse
s = "12345"
print ''.join(reversed(s))

# Split
word = "guru99 career guru99"
print word.split(' ')
print word.split('r')

# Immutability demo
x = "Guru99"
print x.replace("Guru99", "Python")

x = x.replace("Guru99", "Python")
print x

Summary

Python

  1. Mastering Python I/O and Module Imports: A Practical Guide
  2. Master Python Strings: Creation, Formatting, and Manipulation
  3. Mastering Java Strings: Creation, Methods, and Best Practices
  4. C Strings Made Easy: Declaration, Input, Output, and Library Functions
  5. Python Print() Function: A Practical Guide with Examples
  6. Mastering Python’s strip() Method: Comprehensive Guide & Practical Examples
  7. Master Python’s str.count(): How to Count Characters & Substrings with Examples
  8. C Strings: How Null-Terminated Character Arrays Work
  9. Mastering Strings in C#: Best Practices & Tips
  10. Mastering Python Strings: Creation, Access, and Manipulation