Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Interesting facts about Python Lists
Next article icon

Interesting Facts About Python strings

Last Updated : 04 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Strings are one of the most commonly used data types in Python. They allow us to work with text and can be used in various tasks like processing text, handling input and output and much more. Python strings come with several interesting and useful features that make them unique and versatile.

Here are Some Interesting Facts About Python Strings

1. Strings are Immutable

Once a string is defined, it cannot be changed.

Python
a = 'Geeks' print(a)  a[2] = 'E' print(a)  

Output:

Traceback (most recent call last):
File "/home/adda662df52071c1e472261a1249d4a1.py", line 9, in
a[2] = 'E'
TypeError: 'str' object does not support item assignment

But below code works fine.

Python
a = 'Geeks' print(a) a = a + 'for'  print(a)  

Output
Geeks Geeksfor 

2. Strings Can Be Concatenated Using the + Operator

We can easily combine (concatenate) strings using the + operator. This simple operation allows you to build longer strings from smaller ones, which is helpful when working with user input or constructing strings dynamically.

Python
first_name = "Krishna" last_name = "Nand" full_name = first_name + " " + last_name print(full_name)   

Output
Krishna Nand 

3. Strings Support String Multiplication

In Python, we can repeat a string multiple times using the * operator. String multiplication can be used to create patterns, repeat text, or perform formatting tasks easily.

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

Output
gfggfggfg 

4. Strings Can Be Accessed with Indexes

You can access individual characters in a string by using their index. The first character has index 0, and the last character can be accessed with a negative index (-1). This allows you to quickly access and work with specific characters within a string.

Python
s = "GFG" print(s[0])   print(s[-1])   

Output
G G 

5. Three ways to create strings

Strings in Python can be created using single quotes or double quotes or a triple quotes. The single quotes and double quotes works same for the string creation. Talking about triple quotes, these are used when we have to write a string in multiple lines and printing as it is without using any escape sequence.

Python
a = 'Geeks' b = "for" c = '''Geeks a portal  for geeks'''  d = '''He said, "I'm fine."'''  print(a) print(b) print(c) print(d)  print(a + b + c)  

Output
Geeks for Geeks a portal  for geeks He said, "I'm fine." GeeksforGeeks a portal  for geeks 

6. Printing single quote or double quote on screen

We can do that in the following two ways:

  • First one is to use escape character to display the additional quote.
  • The second way is by using mix quote, i.e., when we want to print single quote then using double quotes as delimiters and vice-versa.

Example-

Python
print("Hi Mr Geek.")  # use of escape sequence print("He said, \"Welcome to GeeksforGeeks\"")      print('Hey so happy to be here')  # use of mix quotes print ('Getting Geeky, "Loving it"')                 

Output
Hi Mr Geek. He said, "Welcome to GeeksforGeeks" Hey so happy to be here Getting Geeky, "Loving it" 

7. Printing escape character

If there is a requirement of printing the escape character(\) instead, then if user mention it in a string interpreter will think of it as escape character and will not print it. In order to print the escape character user have to use escape character before ‘\’ as shown in the example.

Python
# Print Escape character print (" \\ is back slash ") 

Output
 \ is back slash  

8. String Comparison is Case-Sensitive

Python string comparisons are case-sensitive. This means that “hello” and “Hello” are considered different strings. This allows for precise string matching, but it’s also important to handle case sensitivity when comparing user input or working with text data.

Python
print("hello" == "Hello") 

Output
False 

9. Strings Can Be Multiline

You can create multiline strings in Python using triple quotes (“”” or ”’). Multiline strings are useful for writing longer pieces of text or docstrings for functions and classes.

Python
s = """This is a multiline string""" print(s) 

Output
This is a multiline string 

10. Strings Can Be Reversed

You can reverse a string using slicing with a step of -1. Reversing strings can be helpful in various tasks, such as palindromes or analyzing strings from right to left.

Python
s = "GeeksForGeeks" print(s[::-1])   

Output
skeeGroFskeeG 

Fun Features in Python Strings

1. Strings Can Be Sliced

Python allows you to slice strings, which means you can extract a portion of the string by specifying a range of indices. String slicing makes it easy to extract parts of a string for further processing or analysis.

Example:

Python
s = "hello" print(s[1:4])   

Output
ell 

2. Strings Can Be Split into Lists

The split() method splits a string into a list based on a specified delimiter (by default, it splits by whitespace). Splitting strings is useful for tasks like parsing sentences or processing user input into individual components.

Example:

Python
s = "Geeks For Geeks" words = s.split() print(words)  

Output
['Geeks', 'For', 'Geeks'] 

3. Strings Support String Formatting

Python provides multiple ways to format strings, such as the old % formatting method, str.format() method, and f-strings (available in Python 3.6+). String formatting makes it easier to construct dynamic strings, especially when dealing with variables.

Example:

Python
name = "Alice" age = 25 print(f"My name is {name} and I am {age} years old.")  

Output
My name is Alice and I am 25 years old. 

String built-in Methods

Python strings come with a variety of built-in methods that allow you to manipulate and interact with text in powerful ways. Here’s a list of some commonly used string methods:

1. lower()

Converts all characters in the string to lowercase.

Example:

Python
s = "GEEKS FOR GEEKS" print(s.lower())  

Output
geeks for geeks 

2. replace(old, new)

Replaces occurrences of a substring (old) with another substring (new).

Example:

Python
s = "Hello World" print(s.replace("World", "Python"))  

Output
Hello Python 

3. find(sub)

Returns the index of the first occurrence of the specified substring (sub). Returns -1 if the substring is not found.

Python
s = "Hello World" print(s.find("World"))   print(s.find("Python"))   

Output
6 -1 

4. count(sub)

Returns the number of occurrences of the specified substring (sub) in the string.

Python
s = "Geeks For Geeks" print(s.count("Geeks"))   

Output
2 

5. isdigit()

Returns True if all characters in the string are digits.

Python
s = "12345" print(s.isdigit())   s = "Hello123" print(s.isdigit())  

Output
True False 

Explore in detail about – Python String Methods



Next Article
Interesting facts about Python Lists

A

Arpit Agarwal
Improve
Article Tags :
  • Python
  • School Programming
  • Interesting-Facts
Practice Tags :
  • python

Similar Reads

  • Interesting facts about Python Lists
    Python lists are one of the most powerful and flexible data structures. Their ability to store mixed data types, support dynamic resizing and provide advanced features like list comprehension makes them an essential tool for Python developers. However, understanding their quirks, like memory usage a
    6 min read
  • Py-Facts - 10 interesting facts about Python
    Python is one of the most popular programming languages nowadays on account of its code readability and simplicity. All thanks to Guido Van Rossum, its creator. I've compiled a list of 10 interesting Facts in the Python Language. Here they are:1. There is actually a poem written by Tim Peters named
    4 min read
  • Python String Interpolation
    String Interpolation is the process of substituting values of variables into placeholders in a string. Let's consider an example to understand it better, suppose you want to change the value of the string every time you print the string like you want to print "hello <name> welcome to geeks for
    4 min read
  • C strings conversion to Python
    For C strings represented as a pair char *, int, it is to decide whether or not - the string presented as a raw byte string or as a Unicode string. Byte objects can be built using Py_BuildValue() as // Pointer to C string data char *s; // Length of data int len; // Make a bytes object PyObject *obj
    2 min read
  • f-strings in Python
    Python offers a powerful feature called f-strings (formatted string literals) to simplify string formatting and interpolation. f-strings is introduced in Python 3.6 it provides a concise and intuitive way to embed expressions and variables directly into strings. The idea behind f-strings is to make
    5 min read
  • Python String Concatenation
    String concatenation in Python allows us to combine two or more strings into one. In this article, we will explore various methods for achieving this. The most simple way to concatenate strings in Python is by using the + operator. Using + OperatorUsing + operator allows us to concatenation or join
    3 min read
  • Python String center() Method
    center() method in Python is a simple and efficient way to center-align strings within a given width. By default, it uses spaces to fill the extra space but can also be customized to use any character we want. Example: [GFGTABS] Python s1 = "hello" s2 = s1.center(20) print(s2) [/GFGTABS]Ou
    2 min read
  • string attribute in BeautifulSoup - Python
    string attribute is provided by Beautiful Soup which is a web scraping framework for Python. Web scraping is the process of extracting data from the website using automated tools to make the process faster. If a tag has only one child, and that child is a NavigableString, the child can be accessed u
    1 min read
  • Convert string to a list in Python
    Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
    2 min read
  • Execute a String of Code in Python
    Sometimes, we encounter situations where a Python program needs to dynamically execute code stored as a string. We will explore different ways to execute these strings safely and efficiently. Using the exec function exec() function allows us to execute dynamically generated Python code stored in a s
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences