---
description: Python String replace() :This tutorial covers Python String Operators; methods like join(), split(), replace(), Reverse(). Changing a string to uppercase, lowercase, or capitalize.
title: "Python Strings: Replace, Join, Split, Reverse"
image: https://www.guru99.com/images/python-string-methods.png
---

[Skip to content](#main)

**⚡ Smart Summary**

Python strings are immutable sequences of characters with many built-in methods for text processing. The examples below demonstrate slicing, string operators, replace(), upper() and lower(), join(), reversing, and split() using runnable code.

- 🧵 **Strings as objects:** Python treats every string as an object, so methods attach directly to string variables.
- 🔤 **Case conversion:** upper(), lower(), and capitalize() reformat letter case without altering the original string.
- 🔗 **Join and split:** join() glues characters with a separator, while split() breaks text into a list of substrings.
- 🔒 **Immutability:** replace() returns a new string because Python strings cannot be changed in place.
- 🤖 **AI assistance:** AI coding assistants generate and explain string methods, speeding up text-processing and NLP preprocessing tasks.

Read More

![Python Strings Replace Join Split Reverse](https://www.guru99.com/images/python-string-methods.png)

In Python, everything is an object, and strings are objects too. A Python string can be created simply by enclosing characters in double quotes.

For example:

var = **“Hello World!”**

## Accessing Values in Strings

**Python does not support a character type**; these are treated as strings of length one, also considered as a substring.

We use square brackets for slicing along with the index or indices to obtain a substring.

```
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 

```

## Various String Operators

There are various string operators that can be used in different ways, such as concatenating a different string.

Suppose if a=guru and b=99, then a+b= “guru99”. Similarly, if you are using a\*2, it will give “GuruGuru”. Likewise, you can use other operators in a string.

| Operator | Description | Example |
| --- | --- | --- |
| \[] | Slice – it gives the letter from the given index | a\[1] will give “u” from the word Guru as such ( 0=G, 1=u, 2=r and 3=u)<pre>x="Guru"<br>print (x[1])</pre> |
| \[ : ] | Range slice – it gives the characters from the given range | x \[1:3] it will give “ur” from the word Guru. Remember it will not consider 0 which is G, it will consider word after that is ur.<pre>x="Guru" <br>print (x[1:3])</pre> |
| in | Membership – returns true if a letter exists in the given string | u is present in word Guru and hence it will give 1 (True)<pre>x="Guru" <br>print ("u" in x)</pre> |
| not in | Membership – returns true if a letter does not exist in the given string | l is not present in word Guru and hence it will give 1<pre>x="Guru" <br>print ("l" not in x)</pre> |
| r/R | Raw string suppresses actual meaning of escape characters. | Print r’\\n’ prints \\n and print R’/n’ prints \\n |
| % – Used for string format | %r – It inserts the canonical string representation of the object (i.e., repr(o)) %s – It inserts the presentation string representation of the object (i.e., str(o)) %d – it will format a number for display | The output of this code will be “guru 99”.<pre>name = 'guru'<br>number = 99<br>print ('%s %d' % (name,number))</pre> |
| + | It concatenates 2 strings | It concatenates strings and gives the result<pre>x="Guru" <br>y="99" <br>print (x+y)</pre> |
| \* | Repeat | It prints the character twice.<pre>x="Guru" <br>y="99" <br>print (x*2)</pre> |

**Don't Miss:**

- [Hello World: Create your First Python Program](https://www.guru99.com/creating-your-first-python-program.html)
- [How to Find Average of List in Python](https://www.guru99.com/find-average-list-python.html)
- [Python time.sleep(): Add Delay to Your Code (Example)](https://www.guru99.com/python-time-sleep-delay.html)
- [How to install PIP on Windows](https://www.guru99.com/how-to-install-pip-on-windows.html)

## Some more examples

You can update a Python string by re-assigning a variable to another string. The new value can be related to a previous value, or to a completely different string altogether.

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

**Output:**

```
Hello
Hello Guru99

```

**Note**: – Slice \[:6] or \[0:6] has the same effect.

## Python String replace() Method

The replace() method returns a copy of the string in which the values of the old string have been replaced with the new value.

```
oldstring = 'I like Guru99' 
newstring = oldstring.replace('like', 'love')
print(newstring)
```

**Output:**

```
I love Guru99

```

## Changing upper and lower case strings

In Python, you can even change the string to upper case or lower case.

```
string="python at guru99"
print(string.upper())

```

**Output:**

```
PYTHON AT GURU99

```

Likewise, you can also use other functions such as capitalize().

```
string="python at guru99"		
print(string.capitalize())
```

**Output:**

```
Python at guru99

```

You can also convert your string to lower case.

```
string="PYTHON AT GURU99"
print(string.lower())
```

**Output:**

```
python at guru99

```

## Using “join” function for the string

The join() function is a more flexible way of concatenating a string. With the join() function, you can add any character into the string.

For example, if you want to add a colon (:) after every character in the string “Python”, you can use the following code.

```
print(":".join("Python"))
```

**Output:**

```
P:y:t:h:o:n

```

## Reversing String

By using the reversed() function, you can reverse the string. For example, if we have the string “12345” and then apply the code for the reversed() function as shown below.

```
string="12345"		
print(''.join(reversed(string)))
```

**Output:**

```
54321

```

## Split Strings

Split strings is another function that can be applied in Python. Let us see it for the string “guru99 career guru99”. First, here we will split the string by using the command word.split and get the result.

```
word="guru99 career guru99"		
print(word.split(' '))
```

**Output:**

```
['guru99', 'career', 'guru99']

```

To understand this better, we will see one more example of split. Instead of a space (‘ ’), we will replace it with (‘r’), and it will split the string wherever ‘r’ is mentioned in the string.

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

**Output:**

```
['gu', 'u99 ca', 'ee', ' gu', 'u99']

```

**Important Note:** In Python, strings are immutable.

Consider the following code:

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

**Output:**

```
Guru99

```

It will still return Guru99. This is because x.replace(“Guru99”,“Python”) returns **a copy of X with replacements made**.

You will need to use the following code to observe the changes.

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

**Output:**

```
Python

```

The above codes are Python 3 examples. If you want to run them in Python 2, please consider the following code.

**Python 2 Example**

```
#Accessing Values in Strings
var1 = "Guru99!"
var2 = "Software Testing"
print "var1[0]:",var1[0]
print "var2[1:5]:",var2[1:5]
#Some more examples
x = "Hello World!"
print x[:6] 
print x[0:6] + "Guru99"
#Python String replace() Method
oldstring = 'I like Guru99' 
newstring = oldstring.replace('like', 'love')
print newstring
#Changing upper and lower case strings
string="python at guru99"
print string.upper()
string="python at guru99"		
print string.capitalize()
string="PYTHON AT GURU99"
print string.lower()
#Using "join" function for the string
print":".join("Python")		
#Reversing String
string="12345"		
print''.join(reversed(string))
#Split Strings
word="guru99 career guru99"		
print word.split(' ')
word="guru99 career guru99"		
print word.split('r')
x = "Guru99"
x.replace("Guru99","Python")
print x
x = "Guru99"
x = x.replace("Guru99","Python")
print x

```

**Output:**

```
var1[0]: G
var2[1:5]: oftw
Hello
Hello Guru99
I love Guru99
PYTHON AT GURU99
Python at guru99
python at guru99
P:y:t:h:o:n
54321
['guru99', 'career', 'guru99']
['gu', 'u99 ca', 'ee', ' gu', 'u99']
Guru99
Python

```

Python has introduced a .format() function which does away with using the cumbersome %d and so on for string formatting.

» Learn more about [Python String split()](https://www.guru99.com/python-string-split.html)

This code is editable. Click Run to Execute  

  

## FAQs

🧩 What are f-strings and how do they format Python strings?

F-strings are literals prefixed with f that embed variables and expressions inside curly braces. Since Python 3.6 they have been faster and more readable than the percent operator or format() method.

🧹 How do you remove whitespace from a Python string?

Use strip() to remove leading and trailing whitespace, lstrip() for the left side, and rstrip() for the right. Passing characters, such as strip(‘xy’), removes those specific characters instead of spaces.

🔎 How do you find the position of a substring in a Python string?

The find() method returns the index of the first occurrence of a substring, or -1 when not found. The index() method works the same but raises ValueError if the substring is missing.

🔢 How do you count occurrences of a substring in a string?

The count() method returns how many times a substring appears, for example ‘guru99 guru99’.count(‘guru99’) returns 2. You can also pass optional start and end indices to count within a slice.

🏁 How do you check if a string starts or ends with specific text?

Use startswith() and endswith(), which return True or False. Both accept a tuple, so text.startswith((‘http’, ‘https’)) tests several prefixes at once. These checks are case-sensitive, so normalize with lower() first.

🧊 What is the difference between comparing strings with == and is?

The == operator compares string values, returning True when characters match. The is operator compares identity, whether two names point to the same object in memory. For everyday text comparison, always use ==.

🤖 How can AI help with Python string manipulation and text processing?

AI coding assistants generate string-handling code from plain-language prompts, explain unfamiliar methods, and suggest cleaner alternatives. In machine learning and NLP, operations like split(), lower(), and replace() normalize raw text before it feeds models.

🧠 Can GitHub Copilot generate and refactor Python string code?

Yes. GitHub Copilot autocompletes methods like replace(), join(), and split() from a comment describing your goal. Agentic AI tools go further, refactoring a whole script — swapping loops for join() or f-strings — then running your tests.

#### Summarize this post with:

ChatGPTPerplexityGrokGoogle AI

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.**

##### Sign up for the newsletter

Subscribe for Free

You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals

[Scroll to top](#wrapper)

×

Search for:

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/python-string-methods.png","url":"https://www.guru99.com/images/python-string-methods.png","width":"700","height":"250","caption":"Python String Methods","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/learning-python-strings-replace-join-split-reverse.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/python","name":"Python"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/learning-python-strings-replace-join-split-reverse.html","name":"Python Strings: Replace, Join, Split, Reverse"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/learning-python-strings-replace-join-split-reverse.html#webpage","url":"https://www.guru99.com/learning-python-strings-replace-join-split-reverse.html","name":"Python Strings: Replace, Join, Split, Reverse","dateModified":"2026-10-07T13:30:53+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/python-string-methods.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/learning-python-strings-replace-join-split-reverse.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/editorial","name":"Editorial Team","url":"https://www.guru99.com/author/editorial","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/favicon-new-logo.png","url":"https://www.guru99.com/images/favicon-new-logo.png","caption":"Editorial Team","inLanguage":"en-US"},"sameAs":["https://www.guru99.com/editorial-team"],"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Python","headline":"Python Strings: Replace, Join, Split, Reverse","description":"Python String replace() :This tutorial covers Python String Operators; methods like join(), split(), replace(), Reverse(). Changing a string to uppercase, lowercase, or capitalize.","keywords":"python","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/editorial","name":"Editorial Team"},"dateModified":"2026-10-07T13:30:53+05:30","image":{"@id":"https://www.guru99.com/images/python-string-methods.png"},"copyrightYear":"2026","name":"Python Strings: Replace, Join, Split, Reverse","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What are f-strings and how do they format Python strings?","acceptedAnswer":{"@type":"Answer","text":"F-strings are literals prefixed with f that embed variables and expressions inside curly braces. Since Python 3.6 they have been faster and more readable than the percent operator or format() method."}},{"@type":"Question","name":"How do you remove whitespace from a Python string?","acceptedAnswer":{"@type":"Answer","text":"Use strip() to remove leading and trailing whitespace, lstrip() for the left side, and rstrip() for the right. Passing characters, such as strip('xy'), removes those specific characters instead of spaces."}},{"@type":"Question","name":"How do you find the position of a substring in a Python string?","acceptedAnswer":{"@type":"Answer","text":"The find() method returns the index of the first occurrence of a substring, or -1 when not found. The index() method works the same but raises ValueError if the substring is missing."}},{"@type":"Question","name":"How do you count occurrences of a substring in a string?","acceptedAnswer":{"@type":"Answer","text":"The count() method returns how many times a substring appears, for example 'guru99 guru99'.count('guru99') returns 2. You can also pass optional start and end indices to count within a slice."}},{"@type":"Question","name":"How do you check if a string starts or ends with specific text?","acceptedAnswer":{"@type":"Answer","text":"Use startswith() and endswith(), which return True or False. Both accept a tuple, so text.startswith(('http', 'https')) tests several prefixes at once. These checks are case-sensitive, so normalize with lower() first."}},{"@type":"Question","name":"What is the difference between comparing strings with == and is?","acceptedAnswer":{"@type":"Answer","text":"The == operator compares string values, returning True when characters match. The is operator compares identity, whether two names point to the same object in memory. For everyday text comparison, always use ==."}},{"@type":"Question","name":"How can AI help with Python string manipulation and text processing?","acceptedAnswer":{"@type":"Answer","text":"AI coding assistants generate string-handling code from plain-language prompts, explain unfamiliar methods, and suggest cleaner alternatives. In machine learning and NLP, operations like split(), lower(), and replace() normalize raw text before it feeds models."}},{"@type":"Question","name":"Can GitHub Copilot generate and refactor Python string code?","acceptedAnswer":{"@type":"Answer","text":"Yes. GitHub Copilot autocompletes methods like replace(), join(), and split() from a comment describing your goal. Agentic AI tools go further, refactoring a whole script \u2014 swapping loops for join() or f-strings \u2014 then running your tests."}}]}],"@id":"https://www.guru99.com/learning-python-strings-replace-join-split-reverse.html#schema-1140182","isPartOf":{"@id":"https://www.guru99.com/learning-python-strings-replace-join-split-reverse.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/learning-python-strings-replace-join-split-reverse.html#webpage"}}]}
```
