Learn About Strings in Python

Introduction

 
In this article, I will explain strings in Python.
 
Definition
 
In Python, string data type is used to print a string statement in the output screen. We use symbol single quotations (or) double quotations to give the same result.
(“c# corner” is the same as ‘c# corner’).
 
Example
  1. print("c# corner")#double quotation  
  2. print('c# corner')#single quotation  
Output
 
Learn About Strings In Python
 

Assign String Variables

 
In Python, to assign string variables, we use any alphabetic sentence(or) alphabet. The variable name is followed by an equal symbol and the string. (a=” welcome”)
 
Example
  1. a=("welcome,'c# corner'")#double quotation and single quotation  
  2. print(a)#To print the output screen (a) assigned string variables.  
Output
 
Learn About Strings In Python
 

Multiline strings

 
In Python, we can assign one more lines to a variable. We use three quotes as the symbol. (“”’ some statement “””)
 
Example
  1. a = '''''Great Wall of China.. ... 
  2. Christ the Redeemer in Rio de Janeiro ... 
  3. Machu Picchu in Peru. ... 
  4. Chichen Itza. ... 
  5. Roman Colosseum. ... 
  6. Taj Mahal in India. ... 
  7. Petra in Jordan.'''  
  8. print(a) 
Output
 
Learn About Strings In Python
 

String in array

 
Arrays are popular in most programming languages, such as Java or C/C++. An array is a separate string value, one by one. The first array is in the “0” position, the last array position is the negative value keyword (-1).
 
Example
  1. a = "welcome,c# corner" #First array is the “0” position   
  2. print(a[1])# "1" array position is the second position.  
  3. print(a[-1])#last position  
  4. print(a[2:5])#position 2 to position 5.   
Output
 
Learn About Strings In Python
 

String length method

 
In python, the len () keyword is used to find many letters present in a string.
 
Example
  1. a = "c# corner"  
  2. print(len(a))# find many letters present in string  
Output
 
Learn About Strings In Python
 

String lower method

 
In Python, the lower () keyword is used to change the string to lowercase.
 
Example
  1. a = "c# corner"  
  2. print(a.upper())#change the string in upper case  
Output
 
Learn About Strings In Python
 

String Upper Method

 
In Python, the upper () keyword is used to change the string to upper case.
 
Example
  1. a = "C# CORNER"  
  2. print(a.lower())#change the string in lower case  
Output
 
Learn About Strings In Python
 

Conclusion

 
In this article, we have seen strings in Python. I hope this article was useful to you. Thanks for reading!


Similar Articles