GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Python String - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Python String

Last Updated : 19 Sep, 2026

Strings are sequence of characters written inside quotes. It can include letters, numbers, symbols and spaces. Python does not have a separate character type.

  • A single character is treated as a string of length one.
  • Strings are commonly used for text handling and manipulation.

Creating a String

Strings can be created using either single ('...') or double ("...") quotes. Both behave the same.

Python
a = 'GFG'  
b = "GeeksForGeeks"  
print(a)
print(b)

Output
GFG
GeeksForGeeks

Multi-line Strings

Use triple quotes ('''...''' ) or ( """...""") for strings that span multiple lines. Newlines are preserved.

Python
s = """I am Learning
Python String on GeeksforGeeks"""
print(s)

s = '''I'm a 
Geek'''
print(s)

Output
I am Learning
Python String on GeeksforGeeks
I'm a 
Geek

Accessing Characters in String

Strings are indexed sequences. Positive indices start at 0 from the left, negative indices start at -1 from the right as represented in below image:

frame_3086
Positive and Negative Indexing

Example 1: Access specific characters through positive indexing.

Python
s = "ABCDEF"
print(s[0])   
print(s[4])  

Output
A
E

Note: Accessing an index out of range will cause an IndexError. Only integers are allowed as indices and using a float or other types will result in a TypeError.

Example 2: Read characters from the end using negative indices.

Python
s = "ABCDEF"
print(s[-3])  
print(s[-5]) 

Output
D
B

String Slicing

Slicing is a way to extract a portion of a string by specifying the start and end indexes. The syntax for slicing is string[start:end], where start starting index and end is stopping index (excluded).

Python
s = "ABCDEF"
print(s[1:4])    
print(s[:3])     
print(s[3:])    
print(s[::-1])  

Output
BCD
ABC
DEF
FEDCBA

Looping Through Strings

Strings are iterable, which means we can access each character one by one using a loop.

Python
s = "ABCDEF"
for char in s:
    print(char)

Output
A
B
C
D
E
F

Explanation: for loop goes through the string in order and each iteration prints the next character.

String Immutability

Strings are immutable, meaning their values cannot be changed after creation. Any modification to a string creates a new string instead of altering the original one.

Python
s = "aBCDEF"
s = "A" + s[1:]  
print(s)

Output
ABCDEF

Explanation: original string "aBCDEF" is not modified. A new string is created by combining "A" with s[1:] (rest of the string).

Deleting a String

Individual characters of a string cannot be deleted because strings are immutable. However, an entire string variable can be removed using del keyword.

Python
s = "ABC"
del s

Note: After deleting a string, if we try to access it, Python raises a NameError because the variable no longer exists in memory.

Updating a String

Strings cannot be changed directly after creation. So any modification results in a new string being created using slicing or methods like replace().

Python
s = "ABCD EF"
s1 = "H" + s[1:]                  
s2 = s.replace("ABC", "abc")  

print(s1)
print(s2)

Output
HBCD EF
abcD EF

Explanation:

  • s1: slice from index 1 onward and prepend "H".
  • s2: replace("ABC", "abc") returns a new string.

Common String Methods

Python provides various built-in methods to manipulate strings. Below are some of the most useful methods:

1. len(): returns the total number of characters in a string (including spaces and punctuation).

Python
s = "GeeksforGeeks"
print(len(s))

Output
13

2. upper() and lower(): upper() method converts all characters to uppercase whereas, lower() method converts all characters to lowercase.

Python
s = "Hello World"
print(s.upper())
print(s.lower())

Output
HELLO WORLD
hello world

3. strip() and replace(): strip() removes leading and trailing whitespace from the string and replace() replaces all occurrences of a specified substring with another.

Python
s = "   ABC   "
print(s.strip())    

s = "Python is fun"
print(s.replace("fun", "awesome"))

Output
ABC
Python is awesome

To learn more about string methods, please refer to Python String Methods.

Concatenating and Repeating Strings

1. Concatenation: Strings can be combined by using + operator.

Python
s1 = "Hello"
s2 = "World"
print(s1 + " " + s2)

Output
Hello World

2. Repetition: A string can be repeated multiple times using *.

Python
s = "Hello "
print(s * 3)

Output
Hello Hello Hello 

Formatting Strings

1. Using f-strings: f-strings allows to directly insert variables and expressions inside a string using {} brackets.

Python
name = "Jake"
age = 22
print(f"Name: {name}, Age: {age}")

Output
Name: Jake, Age: 22

2. Using format(): format() method allows inserting values into placeholders {} inside a string.

Python
s = "My name is {} and I am {} years old.".format( "Emily", 22)
print(s) 

Output
My name is Emily and I am 22 years old.

String Membership Testing

in keyword is used to check whether a substring exists inside a string. It returns True if the substring is found, otherwise it returns False.

Python
s = "GeeksforGeeks"
print("Geeks" in s)
print("GfG" in s)

Output
True
False
Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.