In Python String type is used to store characters. String type stores characters as by of Unicode. A string may contain nothing or multiple characters where the size is related to the character count. The string data can not be used for mathematical or arithmetic calculations. In this tutorial, we examine string types with different aspects.
A string or string variable can be created by simply assigning a value to a variable. As python does not provide type specifiers simply assigning a string value creates a string variable or string type. In the following example, we create a string variable named
name . A double quote is used to specify the string value.
name = "Ahmet"
Alternatively, we can use a sentence that contains punction marks and spaces as a string value. In the following example, we create the variable named
sentence which contains letters, punction marks, spaces, dots, etc.
sentence = "I like pythontect.com"
One of the most practical ways to print and check string values is using the
print() method in order to put string values to the standard output or command-line interface.
sentence = "I like pythontect.com" print(sentence)
Alternatively, a single quote can be used to specify the string value like below.
name = 'Ahmet'
A string variable can be edited or updated after definition or creation. Just put the string value you want to update and assign to the existing string variable. In the following example first, we set the string value as “Ahmet” and then update it to “Elif”.
name = "Ahmet" print(name) name = "Elif" print(name)
The output is like below where first the string “Ahmet” is printed and then the value of the update “Elif” is printed.
Access String Characters
In Python, a string is interpreted as a character array and each character of the string can be easily accessed by using the index of the character. The index numbering starts from 0. Each character which can be space, dot, etc is assumed as a character and takes one index. The square brackets are used to specify the index number we want to access. For example
name is used to access the character with index number 2 which is 3rd character of the name variable.
name="Ahmet" print(name) print(name) print(name)
Alternatively, we can access a range of characters by using index range specifiers. The square brackets are used to specify the index range where the START and END index numbers are provided by separating them
: . For example
name[0:2] is used to access the first 2 characters of the name string variable. The END index numbered character is not displayed.
name="Ahmet" print(name[0:2]) print(name[2:4])
Iterate String Characters
As the string is interpreted as a character array or character list we can iterate over these characters by using loops. The for loop can be used to iterate over every character of a string.
name = "Ahmet" for x in name: print(x)
A h m e t
Concatenate Two or More Strings
Concatenation is the process of adding two or more strings together. We can easily add two or more strings by using the
a = "I" b = "like" c = "pythontect.com" sentence = a + " " + b + " " + c print(sentence)
I like pythontect.com
String Escape Sequencing
A string value may contain different characters where escape characters are very useful to define path or quote signs. Escape characters are not directly displayed like a regular string character. They are interpreted with the character next to them.
Escape Single Quote:
sentence1 = 'I like the \'pythontect\'' sentence2 = "I like the \'pythontect\'" print(sentence1) print(sentence2)
I like the 'pythontect' I like the 'pythontect'
Escape Double Quote:
sentence1 = 'I like the \"pythontect\"' sentence2 = "I like the \"pythontect\"" print(sentence1) print(sentence2)
I like the "pythontect" I like the "pythontect"
Path Escape Sequence:
linux_path = '/home/ismail/Downloads' windows_path= "C:\\Users\\ismail\\Downloads" print(linux_path) print(windows_path)
Generally, strings are created and used as single lines. But we can also create and use multi-line strings in Python. The triple quotes can be used to create a multiline string that spans multiple lines. The first appearance of the triple quote is the start of the multiline string and the second appearance of the triple quote is the end of the multiline string. Every character between these is interpreted as string characters. The multiline string is also called a block string.
multiline_string = ''' This is a line. This is second line. This is another multiline. ''' print(multiline_string) another_multiline_string = """ This is a line. This is second line. This is another multiline. """ print(another_multiline_string)
This is a line. This is second line. This is another multiline.
A string may contain 0 or more characters. The character count is also named string length. The string length and be calculated by using the
name = "Ahmet" print(len(name))