Data structures are used to store and organize data efficiently. They allow you to quickly access and manipulate the data. Python includes many built-in data structures for easy data management.
In this article we will learn about one of Python’s most important data structures: Strings.
Working on real-world projects will make you realize how significant strings are as a data type in Python. Since working with strings seems simple but solving problems is complex, the majority of problem statements in interviews and written examinations are based on string manipulation.
In this article we cover everything about strings.
Strings are a collection of Unicode characters that are used specifically in Python. A string is the language or text we use to communicate. However, machines can only read binary, they cannot comprehend text. As a result, ASCII numbers are initially generated from characters before being transformed to binary representation. However, ASCII is exclusively used for the English language, and when programming languages are used in other nations, they are translated to 16-bit characters known as Unicode.
Python offers several methods for creating strings, including single, double, and triple quotes. When constructing a string, if the string contains any kind of quote, you should use a separate kind of quote in the declaration to prevent an error. Try the example below in any Python IDE or code editor for a better understanding.
1 2 3 4 5 6 7 8 9 10
s = 'Good Morning' #single quotes s = 'It' s been a great day ' #syntax error (It confuses where string starts and ends) s = "It's been a great day" #correct way s = '' 'Good Morning' '' #multi - line strings
When declaring or referencing a lengthy paragraph use triple quotes. Any other data type can be declared as a string or converted using an internal function.
1
2
a = 99 #int
print(str(a)) #int to str
To address a specific use case, it may occasionally be essential to access portions of a string. We shall discuss indexing and slicing, which enable us to extract a specific section of the text as output.
Indexing a string with zeros is the same as indexing an array. Simply put, the zero indexes in a string represent the first character, and as the index rises, the string advances. Positive and negative indexing are the two categories of indexing in Python.
Positive indexing is moving from zero to the end length of a string. It is also known as zero-based indexing.
1
2
3
4
5
6
s = 'GoodMorning'
print(s[0])
print(s[2])
print(s[4])
print(s[len(s) - 1])
print(len(s))
output:
1
2
3
4
5
G
o
M
g
11
Note: If access s(11), program returns a Index error
Accessing string elements using a negative index means going backward in time. It usually takes a long time to access the last character in a string if we use positive indexing since we have to calculate the length of the string and then take one off of it. In this situation, we can just use the negative indexing.
1
2
3
s = 'GoodMorning'
print(s[-1])
print(s[-2])
output:
1 2
g n
The concept of slicing, which is accomplished using the colon operator, comes into play when we have long strings, such as sentences or paragraphs, and we want to access specific pieces, like whole words, but we cannot do so merely using indexing. To ensure that the output is a portion of the string from a start index to an end index less one, we must specify the starting index and end index in square brackets that are separated by colons.
1
2
s = 'Good Morning'
print(s[1: 4])
output:
1
ood
Start with the first index in the example above and retrieve the character all the way to the third index. Now that there are various slicing variations, let’s examine them practically using the code sample below.
1
2
3
4
5
6
s = 'GoodMorning'
print(s[1: ])
print(s[: 4])
print(s[: ])
print(s[0: 8: 3])
print(s[::-1])
output:
1 2 3 4 5
oodMorning #It will print the entire string starting from index 1 Good #It will print the entire string starting from index 0 to 3 GoodMorning #It will print the entire string Gdr #It will print zero index and skip two index and take third index till eighth index gninroMdooG #It will print the string in reverse order
Since strings in Python are immutable data types, attempting to change one will result in a type error. The existing string cannot be changed or added to, but it can be assigned to a separate variable to produce a new string. Let’s examine the practical effects of deleting and altering strings to see whether we experience any errors.
Since strings are immutable, we cannot directly reassign the value of the particular string in the below snippet.
1
2
3
4
s = 'GoodMorning'
s[0] = "M"
#This operation is not allowed and it will
return not support Item assignment error
Instead, the below combination will work
1
2
3
s = 'GoodMorning'
a = "M" + s[1: ]
print(a)
output:
1
MoodMorning
Deleting the strings won’t work well and the below statements will generate errors saying we can’t delete the string.
1
2
del s[0]
del s[: 3: 2]
It is crucial to comprehend that strings cannot be changed; nothing can be added or removed from an existing string.
Concatenation, as it is also known, allows us to link any amount of strings by using the addition operator.
1 2
s = "Good" + "-" + "Morning" print(s)
output:
1
Good - Morning
To repeat a certain string before the multiplication operator any number of times is known as string repetition. When we want a pattern, we use it.
1
2
s = "Good"
print(s * 3)
output:
1
GoodGoodGood
Relational operators display the relationship between two terms and determine whether a particular condition is true or false, producing a boolean result.
1 2
print("Hello" == "World") print("Hello" != "World")
output:
1 2
False True
When we apply the larger than and less than operators the situation becomes more intriguing.
1 2
print("Mumbai" > "Pune") print("Goa" < "Indore")
output:
1 2
False True
You might be wondering how it compares two strings. As a result, it lexicographically compares a string. As P follows M in the alphabetical order in the first case, the statement is false. Lowercase characters are placed after uppercase because occasionally people mistake the two case types.
When you use the AND, OR, and NOT logical operators on strings, Python returns false for empty strings and true for non-empty strings.
1 2 3 4 5 6 7 8
print("Hello" and "World") #True and True - > True(O / P - > World) print("" and "World") #False and True - > False(O / P - > "") print("" or "World") #False OR True - > True(O / P - > "World") print(not "Hello") #opposite of True - > False print(not "") #True
Slicing allows us to loop over strings starting at any index. To access distinct characters or create a pattern in a string, we can use both a where and a for loop.
1 2 3
s = 'GoodMorning' for i in s: print(i)
output:
1 2 3 4 5 6 7 8 9 10 11
G o o d M o r n i n g
Python’s in and NOT in membership operators are used to determine whether any element is present in any sequence data structure.
1 2 3
s = 'GoodMorning' if 'M' in s: print('true')
output:
1
true
The most important parts of a string are string functions which you use everywhere whenever you build any project. Now we will study all-important string functions which are frequently used.
Common Functions:
These are the same functions that are available for all other iterator data types, including list, tuple, set, and string.
length - It provides the string’s length.
minimum - This function returns the smallest character in a string, as determined by ASCII.
maximum - The largest character found in the string, as determined by ASCII, is provided by the third option, maximum.
sorted - The string is sorted in either ascending or descending order using the ASCII character sequence. This function always returns a list of characters as its result.
1 2 3 4 5 6
s = 'GoodMorning' print(len(s)) print(min(s)) print(max(s)) print(sorted(s)) print(sorted(s, reverse = True))
output:
1
2
3
4
5
11
G
r
['G', 'M', 'd', 'g', 'i', 'n', 'n', 'o', 'o', 'o', 'r']
['r', 'o', 'o', 'o', 'n', 'n', 'i', 'g', 'd', 'M', 'G']
The functions we’ll look at here only work with strings.
The title function capitalizes the first letter of each word, whereas the capitalize function capitalizes the first letter of a string.
1 2 3
s = "its raining outside" print(s.capitalize()) #O / P - > Its raining outside print(s.title()) #O / P - > Its Raining Outside
In string each character is converted in the upper function to lowercase characters. Swap case changes an upper to a lower case character, and vice versa.
1 2 3 4
s = "Its Raining Outside" print(s.upper()) #O / P - > ITS RAINING OUTSIDE print(s.lower()) #O / P - > its raining outside print(s.swapcase()) #O / P - > iTS rAINING oUTSIDE
It provides the number of any substrings that are present in a string. It outputs 0 if the substring is missing from the string. It is used to determine how often a given substring occurs in a string.
1 2 3
s = "Its Raining Outside" print(s.count("i")) #3 print(s.count("ing")) # 1
Both functions operate in exactly the same manner. Both locate the position of a substring within a string. The sole distinction between the two functions is that while index gives an error when the substring is absent, string find produces a negative value when this occurs.
1 2 3 4 5
s = "Its Raining Outside" print(s.find("ing")) #8 print(s.index("ing")) # 8 print(s.find("down")) # - 1 print(s.index("down")) #error
In this article we have learned about string literals in Python. We have started by getting ideas about string and covered how to use, create, and play with string and string functions. Hope you enjoyed it.