Python Strings and Their Methods — A Complete Beginner’s Guide


In this article, we will be covering strings and their methods.
As we have already discussed in the earlier article on various datatypes, str
is a datatype used for storing characters and words.
A string is a collection of characters. For example, the word "Python"
is a sequence of the characters "P"
, "y"
, "t"
, "h"
, "o"
, "n"
. Strings are considered immutable, i.e., once a string is created, we cannot change it directly; we have to create a new string to make changes to the original string.
The position of the characters in a string starts from zero. For example, in the word "Python"
:
"P" → 0
"y" → 1
"t" → 2
"h" → 3
"o" → 4
"n" → 5
Creating Strings
We can create strings in Python using:
Single quotes
Double quotes
Triple quotes
1. Single quotes:
We can create strings by placing the value inside single quotes.
Example:
name = 'Python'
print(name)
# Output - Python
2. Double quotes:
We can create strings by placing the value inside double quotes.
Example:
name = "Python"
print(name)
# Output - Python
3. Triple quotes:
Sometimes we need to create strings that span multiple lines. In such cases, we use triple quotes.
Example:
a = """Welcome to the article
In this article, we will learn
about strings and also
different methods"""
print(a)
Output:
Welcome to the article
In this article, we will learn
about strings and also
different methods
Note: While using multi-line strings, make sure you are assigning them to a variable; otherwise, they will be treated as comments.
Reading Input from the User
We can read text from the user using the input()
function:
name = input("Enter your name: ")
print(f"Hello, {name}")
Example Output:
Enter your name: Prajitha
Hello, Prajitha
a = int(input("Enter a value: "))
print(a)
Example Output:
Enter a value: 1
1
If we do not specify a datatype when taking input, Python treats it as a string by default. If we want to take an integer input, we must explicitly cast it using int()
, as in the second example.
The text inside the quotes of the input()
function appears on the screen as a prompt. It is optional—if you don’t want a prompt, you can simply write:
a = int(input())
print(f"a : {a}")
1
a : 1
Concatenation
As mentioned earlier, strings are immutable. To add two or more strings, we use concatenation, which creates a new string:
str1 = "Hello"
str2 = "World"
str3 = str1 + " " + str2
print(str3) # Output - Hello World
String Methods
Here are some commonly used string methods:
upper()
lower()
capitalize()
title()
swapcase()
find()
index()
replace()
strip()
split()
startswith()
endswith()
isalnum()
isalpha()
isdigit()
islower()
isupper()
len()
Let’s look at them in detail:
1. upper()
Converts text to uppercase.
a = "string methods"
print(a.upper()) # Output - STRING METHODS
2. lower()
Converts text to lowercase.
a = "STRING METHODS"
print(a.lower()) # Output - string methods
3. capitalize()
Capitalizes the first letter and converts all other letters to lowercase.
a = "heLLo WOrld"
print(a.capitalize()) # Output - Hello world
4. title()
Capitalizes the first letter of every word into uppercase.
a = "converts first character into uppercase"
print(a.title()) # Output - Converts First Character Into Uppercase
5. swapcase()
Swaps uppercase letters to lowercase and vice versa.
text = "Swaps tHE caSES"
print(text.swapcase()) # Output - sWAPS The CAses
6. find()
Finds the position or index of a character or substring. Returns -1
if not found.
text = "Finds the position of a letter"
print(text.find("p")) # Output - 10
print(text.find("the")) # Output - 6
print(text.find("x")) # Output - -1
7. index()
Similar to find()
, but raises an error if the substring is not found.
text = "Finds the position of a letter"
print(text.index("F")) # Output - 0
print(text.index("position")) # Output - 10
print(text.index("x")) # Raises ValueError
8. replace()
Replaces a substring with another.
text = "I love C"
print(text.replace("C", "Python")) # Output - I love Python
9. strip()
Removes leading and trailing spaces.
text = " Python "
print(text.strip()) # Output - Python
10. split()
Splits the string based on a delimiter.
text = "I am 20 years old"
print(text.split()) # Output - ['I', 'am', '20', 'years', 'old']
text = "a#b#c#d#e"
print(text.split("#")) # Output - ['a', 'b', 'c', 'd', 'e']
11. startswith()
Checks if a string starts with a specific substring.
text = "Python has a simple syntax to code"
print(text.startswith("C")) # Output - False
print(text.startswith("Python")) # Output - True
12. endswith()
Checks if a string ends with a specific substring.
text = "Python has a simple syntax to code"
print(text.endswith("program")) # Output - False
print(text.endswith("code")) # Output - True
13. isalnum()
Returns True
if the string contains only letters and numbers.
a = "Python3"
print(a.isalnum()) # Output - True
a = "Python 3"
print(a.isalnum()) # Output - False
14. isalpha()
Returns True
if the string contains only letters.
a = "Python3"
print(a.isalpha()) # Output - False
a = "Python"
print(a.isalpha()) # Output - True
15. isdigit()
Returns True
if the string contains only digits.
a = "A123"
print(a.isdigit()) # Output - False
a = "1234"
print(a.isdigit()) # Output - True
16. isupper()
Returns True
if all characters are uppercase.
text = "PYTHON"
print(text.isupper()) # Output - True
17. islower()
Returns True
if all characters are lowercase.
text = "python"
print(text.islower()) # Output - True
18. len()
Returns the number of characters in a string.
text = "Prajitha"
print(len(text)) # Output - 8
Summary
Strings in Python are just pieces of text — like words, sentences, or even a single character. You can make them using single quotes, double quotes, or triple quotes (for multi-line text). Since strings are immutable (they can’t be changed directly), Python gives us lots of handy built-in methods to work with them. You can change their case (upper()
, lower()
), make them pretty (capitalize()
, title()
), find words or letters (find()
, index()
), replace text (replace()
), clean up spaces (strip()
), split them into parts (split()
), or check what they’re made of (isalnum()
, isalpha()
, isdigit()
). Mastering these tools will make working with text in Python a lot easier and way more fun!
Note:
The best way to master strings is by practicing them in different ways — try out the examples, mix them up, and create your own variations. Python has many other string functions beyond the ones listed here, so feel free to explore the documentation and experiment if you’re curious. The more you practice, the more comfortable and creative you’ll get with strings!
Subscribe to my newsletter
Read articles from Tammana Prajitha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
