× About Us How it works Pricing Student Archive Subjects Blog Contact Us

Enrich your knowledge with our informative blogs

What are strings in python?

Strings in Python is an immutable collection of characters. It’s like an array of characters that cannot be modified in any case but can only be accessed. 

Here the Unicode characters can be written in single quotes, double quotes and even triple quotes. You can skip a nested quote with the help of a backslash (“\”). 

Here are a few examples of valid string literals in Python: 

# String with double quotes 

print(“Welcome to TEL Gurus!”) 

# String with single quotes 

print (‘Welcome to TEL Gurus!’) 

# String with triple quotes 

print (‘ ‘ ‘Welcome to TEL Gurus!’ ‘ ‘) 

# String with triple double-quotes 

print (” ” “Welcome to TEL Gurus!” ” “) 

Here’s the output screen 

1. # Online Python compiler (interpreter) to run Python online.
2. # Write Python 3 code in this online editor and run it.
3. print(“Welcome to TEL Gurus!”)
4. print(‘Welcome to TEL Gurus!’)
5. print(”’Welcome to TEL Gurus!”’)
6. print(“””Welcome to TEL Gurus!”””)

Assigning string to a variable  

 You can easily assign a string to a variable. This makes the accessing easier and faster. 

You can use ‘+’ to join two strings. This is called string concatenation.  

For example:  

str1 = ‘This is a beautiful place’ 

str2 = ‘We are here to help! 

print (str1) 

print (str2) 

print (str1 +’. ‘+ str2) 

Here’s the output  

/////////////////////// image ///////////////////////////////////

Assessing String elements  

The elements of the strings in Python can easily be accessed using an index.  

The first character of the string has an index 0 and followed by 1,2,3.. and so on. 

You can even access the string from the back using index as -1, -2 etc.  

A string element with index [-1] means the last element of the string. 

Let’s understand the same using a string “TELGURUS”. 

  • First, we’ll create a variable that holds this string value. 
  • Next, we can access the characters using index placed in square brackets.  

str1 = “TEL GURUS” 

T  E  L     G  U  R  U  S 
0  1  2  3  4  5  6  7  8 
-9  -8  -7  -6  -5  -4  -3  -2  -1 


To access the first element of the string we use,  

str1[0] will give T  

and second element,  

str[1] will give E 

To access the elements from the end, we use negative numbers. 

str1[-1] will give S,  str1[-2] will give U. 

Length of a string  

To get the length can be calculated by using a len() function. 

len(<string name>) will give you a number that states the length of the string. 


 This helps you to slice a string and obtain a substring from it. 

Syntax followed is  

string_name [start_index: end_index] 

Here start_index is included but end_index is not included.  

Let’s apply all the concepts learnt above in a program. 

str1 = ‘TEL GURUS’ 

print (“The First Letter of string is  ” + str1[0]) 

print (“The Second Letter of String is ” + str1[1]) 

print (“The last letter is ” + str1[-1]) 

print (“The brand name is “+ str1[0:3]) 

print (“The length of string is “+ str(len(str1))) 

Here’s the output  

/////////////////////// image ///////////////////////////////////

If you look carefully at the program we have used another function str() in the last line while calculating the length of the string.   

It is done because, the len() function will return a number and on concatenating a string and a number, the interpreter will give an error.   

The number needs to be converted into string so as to make it eligible for concatenation.  

So, here comes str() function that converts any number into a string format.  

Escape sequences  

Generally, you aren’t able to print a string with the quotes within the quotes. For that, there are some escape sequences. 

  • Using a backlash “\” enables the interpreter to take quotes into account. 
  • “\n” is used for new line. 
  • “\t” is used for giving a tab space. 
  • “\b” is used as backspace. 

Read More – Coding and Programing Questions

View More – Useful links for Your Child’s Development 

Unveil your gateway to a lucrative career!
Unveil your gateway to a lucrative career!

Unleash the power of true logic building with Real-time instructions and live coding exposure.

Book A Demo Class

Tel Guru
Tel Guru

Register For The Demo Class