- Home
- /
- Python String Methods
Python Tutorial
Introduction
Python Data Types & Oper...
Python Strings
Python Lists
Python Tuples
Python Sets
Python Dictionaries
Conditional Statements
Python Loops
Python Functions
Python Modules
Python OOPS
Advanced Topics
File Handling
- Home
- /
- Python String Methods
Python String Methods
Python provides a variety of built-in string methods that help manipulate, analyze, and transform string data.
upper() – Convert to Uppercase
Converts all characters in a string to uppercase.
word = "codingIsFun"
print(word.upper())
Output:
CODINGISFUN
lower() – Convert to Lowercase
Converts all characters in a string to lowercase.
word = "CodingIsFun"
print(word.lower())
Output:
codingisfun
strip() – Remove Leading/Trailing Spaces
Removes extra spaces at the beginning and end of the string.
text = " Learn Python "
print(text.strip())
Output:
Learn Python
rstrip() – Remove Trailing Characters
Removes specific characters from the end of the string.
greet = "Hello!!!"
print(greet.rstrip("!"))
Output:
Hello
replace() – Replace Part of String
Replaces a part of the string with another.
title = "Learn With Fun"
print(title.replace("Fun", "Arshyan"))
Output:
Learn With Arshyan
split() – Split into List
Splits the string based on a delimiter and returns a list.
line = "Python Programming Language"
print(line.split(" "))
Output:
['Python', 'Programming', 'Language']
capitalize() – Capitalize First Letter
sentence = "welcome to python"
print(sentence.capitalize())
Output:
Welcome to python
center() – Center Align the Text
msg = "Centered Text"
print(msg.center(30, "-"))
Output:
--------Centered Text---------
count() – Count Occurrences
phrase = "banana banana banana"
print(phrase.count("banana"))
Output:
3
endswith() – Check Suffix
url = "learnwitharshyan.com"
print(url.endswith(".com"))
Output:
True
find() – Find First Occurrence
bio = "Arshyan loves teaching Python."
print(bio.find("Python"))
Output:
21
index() – Get Index or Raise Error
bio = "Arshyan is a Python mentor."
print(bio.index("Python"))
Output:
13
isalnum() – Check Letters and Numbers
code = "Python3"
print(code.isalnum())
Output:
True
isalpha() – Only Letters
name = "Arshyan"
print(name.isalpha())
Output:
True
islower() – All Lowercase
data = "all small"
print(data.islower())
Output:
True
isprintable() – Check Printable Characters
test = "VisibleText\n"
print(test.isprintable())
Output:
False
isspace() – Check if Only Spaces
gap = " "
print(gap.isspace())
Output:
True
istitle() – Title Case Check
book = "Learn With Arshyan"
print(book.istitle())
Output:
True
isupper() – All Uppercase
slogan = "BE THE BEST"
print(slogan.isupper())
Output:
True
startswith() – Check Prefix
note = "Learn With Arshyan"
print(note.startswith("Learn"))
Output:
True
swapcase() – Flip Case
lang = "PyThOn LoVeRs"
print(lang.swapcase())
Output:
pYtHoN lOvErS
title() – Title Case Every Word
quote = "coding is a superpower"
print(quote.title())
Output:
Coding Is A Superpower