Usually, a string is only some text (sequence of characters). In Python, a string is denoted by ” (double quotes) or'(single quotes). The Python programming language will be used to demonstrate how to generate, access, use, and manipulate strings.
1. Python: How to construct a String
In Python, there are numerous ways to produce strings.
1. The string str in the following code shows that we can use'(single quotes).
2. As seen in the string str2 in the source code below, we can use ” (double quotes).
3. Python supports multi-line strings by using triple double quotes “”” and triple single quotes “‘. Look at the strings str3 and str4 in the example below.
# lets see the ways to create strings in Python str = 'beginnersbook' print(str) str2 = "Chaitanya" print(str2) # multi-line string str3 = """Welcome to Beginnersbook.com""" print(str3) str4 = '''This is a tech blog''' print(str4)
beginnersbook Chaitanya Welcome to Beginnersbook.com This is a tech blog
2. How to use Python to retrieve strings
Since a string is nothing more than an array of characters, we can retrieve its characters using the string’s indexes. The indexes run from 0 to length-1, much like arrays.
If you attempt to access a character that is not in the range, you will receive an IndexError. For instance,
This error will appear if you attempt to access the eighth character of a string that is six characters long.
If you do not utilize integers as indexes—for instance, if you use a float as an index—you will receive a TypeError.
str = "Kevin" # displaying whole string print(str) # displaying first character of string print(str) # displaying third character of string print(str) # displaying the last character of the string print(str[-1]) # displaying the second last char of string print(str[-2])
Kevin K v n i
3. String operations in Python
Let’s look at the string operations that can be done.
3.1. Slicing operation for obtaining a substring in Python
To extract a substring from a string, we can cut it. Understanding Python’s positive and negative indices is necessary to comprehend the idea of slicing (see the example above to understand this). Let’s see the few slicing instances.
str = "Beginnersbook" # displaying whole string print("The original string is: ", str) # slicing 10th to the last character print("str[9:]: ", str[9:]) # slicing 3rd to 6th character print("str[2:6]: ", str[2:6]) # slicing from start to the 9th character print("str[:9]: ", str[:9]) # slicing from 10th to second last character print("str[9:-1]: ", str[9:-1])
The original string is: Beginnersbook str[9:]: book str[2:6]: ginn str[:9]: Beginners str[9:-1]: boo
3.2 Python String concatenation
Python’s + operator is used to concatenate strings. To further grasp this, let’s look at an example:
str1 = "One" str2 = "Two" str3 = "Three" # Concatenation of three strings print(str1 + str2 + str3)
Note that using the + operator with numbers adds them, but using it with strings concatenates them. However, it will give a TypeError if you attempt to use this between a string and a number.
s = "one" n = 2 print(s+n)
TypeError: must be str, not int
3.3 String repetition – Operator for replication
To repeat a string a predetermined number of times, use the * operator.
str = "ABC" # repeating the string str by 3 times print(str*3)
3.4 Python String Membership Operators in
This determines whether or not a string is contained in another string. If the complete string is found, it returns true; otherwise, it returns false.
not in: It functions inexactly the same way as the “in” operator does. If the provided string contains the string, it returns true; otherwise, it returns false.
str = "Welcome to beginnersbook.com" str2 = "Welcome" str3 = "Chaitanya" str4 = "XYZ" # str2 is in str? True print(str2 in str) # str3 is in str? False print(str3 in str) # str4 not in str? True print(str4 not in str)
True False True
3.5 Relational Operators on Strings in Python
The ASCII values of the characters serve as the basis for the relational operators’ action on strings.
For example, the ASCII value of an is 97, b is 98, etc.
A has the ASCII value of 65, B of 66, and so forth.
str = "ABC" str2 = "aBC" str3 = "XYZ" str4 = "XYz" # ASCII value of str2 is > str? True print(str2 > str) # ASCII value of str3 is > str4? False print(str3 > str4)