A string is a sequence of characters.
Generally, computers do not deal with characters, they deal with numbers. Even though you may see characters on your screen, internally it is stored and manipulated as a combination of 0’s and 1’s.
The conversion of a character to a number is called encoding, and the reverse process is called decoding. ASCII and Unicode are some of the popular encoding techniques.
In python, a string is a sequence of Unicode characters.
Creating a string
There are multiple ways we can create a string object in Python. It can be created using a double quote, single quote or triple quote as well as using the str() function.
Example 1: Creating a string in python, we are creating string objects using different quotes.
str_obj = "www.notepub.io" print(str_obj) # Output: www.notepub.io str_obj = '''www.notepub.io''' print(str_obj) # Output: www.notepub.io str_obj = 'www.notepub.io' print(str_obj) # Output: www.notepub.io
Example 2: Creating a string in python, we are creating string objects using the str() function.
str_obj = str("www.notepub.io") print(str_obj) # Output: www.notepub.io
Accessing characters of a string
We can access individual characters using indexing and a range of characters using slicing. Index starts from zero. When we try to access a character out of the index range, it raises an index error. Python also allows negative indexing for its sequences.
Accessing characters of a string using Indexing
Example 1: In this example of accessing characters of a string in python, we show how to access individual elements of string using indexing.
# Indexing str_obj = str("www.notepub.io") print(str_obj[0]) print(str_obj[1]) print(str_obj[2]) print(str_obj[3]) #Output: # w # w # w # .
In the above example, the first character is accessed by index value 0 and the second by index value 1 and so on and so forth.
Accessing characters of a string using Negative Indexing
Example 1: In this example of accessing characters of a string in python, we have shown how to access individual elements of a string using negative indexing. Negative indexing values start from -1 and -1 is the last character of a string. Indexing value increase as we move toward the start of the string.
# Negative Indexing str_obj = str("www.notepub.io") print(str_obj[-1]) print(str_obj[-2]) print(str_obj[-3]) print(str_obj[-4]) # Output: # o # i # . # b
In the above example, the last character is accessed by index value -1 and the second last by index value -2 and so on and so forth.
Accessing characters of a string using Range
Example 1: In this example of accessing characters of a string in python, we have shown how to a range of characters of a string using a concept called slicing. For slicing, we need to mention a range, like a start index value, colon, and end index value.
# slicing str_obj = str("www.notepub.io") print(str_obj[0:5]) # Output: www.n
In the above example, we have sliced and extracted a substring in a range of 0-4. In all the cases, if we supply an index value which is greater than string length then it throws an index out of range error.
Delete a string
As we know, strings are immutable, which means that elements of a string can’t be changed once it has been assigned. However, we can simply reassign different strings to the same name. Python internally creates a new object and assigns the value to it.
Example 1: In this example of deleting characters from a string, we have shown that we can’t edit or modify a string object once it is assigned.
str_obj = str("www.notepub.io") str_obj[0] = 'h' # Output: TypeError: 'str' object does not support item assignment
In the above example, it throws an error stating that the str object does not support item assignment.
Example 2: In this example of deleting a string, we have shown how to delete the entire string object.
str_obj = str("www.notepub.io") del str_obj print(str_obj) # Output: NameError: name 'str_obj' is not defined
String Operations
String data structure supports many operations such as string concatenation, iteration, etc.
String Concatenation
String concatenation is a way through which we can concatenate two or more strings together. Python internally creates a new string object and copies the content of strings together and returns a new string object.
Example 1: In this example of a string concatenation, we have shown how to concatenate two strings together.
str_obj1 = str("https://") str_obj2 = str("www.notepub.io") # Using the string concatenation of two strings str_obj3 = str_obj1 + str_obj2 print(str_obj3) # Output: # https://www.notepub.io
Example 2: In this example of a string concatenation, we have shown how to repeat the same string multiple times and as a return, get a new string object.
str_obj1 = str("https://") str_obj2 = str("www.notepub.io") # Using the string concatenation of two strings str_obj3 = (str_obj2 + " ") * 3 print(str_obj3) # Output: # www.notepub.io www.notepub.io www.notepub.io
Iterating through String
String iteration is very useful when we want to search for a character or substring within a string.
Example 1: In this example, we will iterate the entire string one by one and search for a character ‘o’ within a given string.
count = 0 str_obj = str("www.notepub.io www.notepub.io www.notepub.io") for ch in str_obj: if ch == 'o': count += 1 print("Number of times 'o' character occured: ",count) # Output: Number of times 'o' character occured: 6
Search substring within a string
It is a frequent need to search whether a character or substring is present within a string or not. As a result, it returns true or false based on the search result.
Example 1: In this example, we have illustrated how to search a substring within a string.
str_obj = str("www.notepub.io www.notepub.io www.notepub.io") status = "note" in str_obj print(status) # Output: True
12,305 total views, 1 views today