![]() |
VOOZH | about |
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.
Once a string is defined, it cannot be changed.
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.
Geeks Geeksfor
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.
Krishna Nand
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.
gfggfggfg
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.
G G
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.
Geeks for Geeks a portal for geeks He said, "I'm fine." GeeksforGeeks a portal for geeks
We can do that in the following two ways:
Example-
Hi Mr Geek. He said, "Welcome to GeeksforGeeks" Hey so happy to be here Getting Geeky, "Loving it"
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.
\ is back slash
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.
False
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.
This is a multiline string
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.
skeeGroFskeeG
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:
ell
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:
['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:
My name is Alice and I am 25 years old.
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:
Converts all characters in the string to lowercase.
Example:
geeks for geeks
Replaces occurrences of a substring (old) with another substring (new).
Example:
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.
6 -1
Returns the number of occurrences of the specified substring (sub) in the string.
2
Returns True if all characters in the string are digits.
True False
Explore in detail about - Python String Methods