MyInternships.in

Python Basics

Python String Methods

Python strings come with many built-in methods that let you clean, search and transform text without writing loops yourself.


Changing Case

The upper() and lower() methods return a new string in all uppercase or all lowercase, which is handy for comparing text regardless of case.

Python
text = "Learn Python"
print(text.upper())   # LEARN PYTHON
print(text.lower())   # learn python

Removing Extra Spaces

strip() removes whitespace from the start and end of a string, which is very useful when cleaning up user input.

Python
raw = "   hello world   "
print(raw.strip())  # "hello world"

Splitting and Joining

split() breaks a string into a list based on a separator, while join() does the opposite — it combines a list of strings into one string.

Python
sentence = "python is fun"
words = sentence.split()
print(words)  # ['python', 'is', 'fun']

joined = "-".join(words)
print(joined)  # python-is-fun

Replacing and Finding Text

Python
msg = "I love Java"
print(msg.replace("Java", "Python"))  # I love Python
print(msg.find("love"))               # 2

Quick Reference Table

MethodWhat it DoesExample
upper()Converts to uppercase"hi".upper() → "HI"
lower()Converts to lowercase"HI".lower() → "hi"
strip()Removes surrounding whitespace" hi ".strip() → "hi"
split()Splits string into a list"a,b".split(",") → ["a","b"]
join()Joins a list into a string"-".join(["a","b"]) → "a-b"
replace()Replaces text"hi".replace("h","H") → "Hi"
find()Returns index of substring"hi".find("i") → 1
format()Inserts values into a string"{}".format("hi") → "hi"
💡

String methods never change the original string — they always return a brand new one, since strings are immutable in Python.

Related Python Topics

Keep learning with these closely related lessons.

Ready to use your Python skills?

Find Python, data science and software internships and fresher jobs across India.

Browse Python Internships