Python Strings

In programming, a string is a series or a sequence of characters. A character is a symbol or a letter or even a number enclosed in quotes. Some example of strings may be – sagar, roll12, %#@!&.

How to create a string in python –

In python, a string can be created by enclosing characters inside single, double or triple quotes.

For example

mystr1 = "mango"

mystr2 = "carl4"

mystr3 = "12"

print(mystr1)

print(mystr2)

print(mystr3)

print(type(mystr3))

Output

mango

carl4

12

<class ‘str’>

Note : The value stored inside variable named ‘mystr3’ appears to be a number but actually is a string.

Multiline strings

A multiline string can be assigned to a variable by enclosing it in triple quotes.
For example

mystr = '''The sun rises in the east
A week has seven days.'''

print(mystr)

Output

The sun rises in the east

A week has seven days.

Difference between single, double and triple quotes

A string can be created by using any type of the quotes (single quotes, double or triple quotes), but there are some differences however.

If there is a string in which a single quote is used –

For example the string is – This is rahul’s pen

If we try to embed this string inside single quotes, the python interpreter will fetch only this part – This is rahul, because after ‘l’ in rahul, there is a single quote. So the python interpreter will assume this to be the end of the string.

mystring = ‘This is rahul’s pen’ (Incorrect)

The interpreter will ignore the part “s pen” as an ending quote has appeared before it. To overcome this, this string should be enclosed inside double quotes, like this –

mystring = “This is rahul’s pen”

Now, if there is a string in which we have used double quotes, then we cannot embed that string inside double quotes. For example –

mystring = “He said – “I am going”” (Incorrect)

In this case, the interpreter would ignore the part ‘I am going’, as an ending double quote has appeared before it.
To overcome this, we should enclose this string inside single quotes. Like this –

mystring = ‘He said – “I am going”‘

Lastly, if we have a string in which we have both single and double quotes, then we can neither enclose it inside single quotes, nor double quotes.

mystring = ‘This is rahul’s pen. He said – “I am going”‘ (Incorrect)

mystring = “This is rahul’s pen. He said – “I am going”” (Incorrect)

To overcome this situation, we should enclose this string inside triple quotes.

mystring = ”’This is rahul’s pen. He said – “I am going””’ (Correct)

Note : If a string is enclosed inside triple quotes and is not assigned to a variable, then it is considered to be a comment.
This is why multiline comments in python are embedded inside triple quotes.

Accessing individual characters

A string in python behaves like an array of bytes representing unicode characters. The characters are by default indexed.
The first character has index, second character has index 1 and so on. For example –

word = "hyderabad"

print(word[4])

Output

r

In this program we have printed the element having index 4, so ‘r’ has been printed.

Loop through a string

If we loop through a string, then the loop will run for every single character present in that string.
For example –

samplestr = "Russia"

for i in samplestr:
    print(i)

Output

R

u

s

s

i

a

The string “Russia” had 6 characters, therefore the loop ran for 6 times on every character.

String Concatenation

Joining one or more strings together is called string concatenation. To concatenate two strings, we simple use the + operator.

For example –

strone = "Ger"

strtwo = "many"

print(strone + strtwo)

Output

Germany

Note : In concatenation, the order of the strings matters. For example, if we change the order of the strings to be concatenated, then the output would also be changed.

strone = "Ger"

strtwo = "many"

print(strtwo + strone)

Output

manyGer

Writing strings together also concatenates them like the + operator does.

strone = "Apple""Mango"

print(strone)

Output

AppleMango

The * operator is used to repeat the string a number of times. For example –

strone = "Apple" 

print(strone * 4)

Output

AppleAppleAppleApple

String slicing

A range of characters can be returned by using slicing. In slicing, we specify the starting index and the ending index to get a part of a string. For example-

word = "himachal"

print(word[2:6])

Output

mach

Notice that the index ranges are separated by a colon and the ending index is not included (the ending range is one less than the mentioned). That is, the sliced string has characters from index 2 to 5 (6 not included).

Negative indexing in strings

A negative index starts from the end of the string and decreases in backward direction.
For example-

word = "cricket"

print(word[-1])

print(word[-2])

Output

t

e

format function()

We cannot combine a string a number directly by using the + operator. But we can combine string with numbers by using the format function. The format function takes the number as an argument, format it and then places it at the position where the placeholders { } are present in the string.

For example-

sentence = "The number of days in a week is {}"

days = 7

print(sentence.format(days))

Output

The number of days in a week is 7

Note : An empty string can be created by specifying nothing inside quotes.

For example-

mystr = ""