Interesting Facts About Python strings
Last Updated : 04 Mar, 2025
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)
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)
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
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])
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)
OutputGeeks 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"')
OutputHi 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 ")
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")
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)
OutputThis 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])
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])
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']
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.")
OutputMy 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())
2. replace(old, new)
Replaces occurrences of a substring (old) with another substring (new).
Example:
Python s = "Hello World" print(s.replace("World", "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"))
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"))
5. isdigit()
Returns True if all characters in the string are digits.
Python s = "12345" print(s.isdigit()) s = "Hello123" print(s.isdigit())
Explore in detail about – Python String Methods
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