Table of Contents
Introduction To Python Strings
The basic definition of python strings is we can say that it is a sequence of characters in python. There are different data types in python for numbers and text. The Data Type of the numbers is int, float, and complex. The Data Type of the text is str( String )
String Declare:- for declare the string in python always use codes (‘ ’).
Example:- Store the Data Science into the X variable
X = ‘ Data Science ’, Here X is Variable and Data Science is a value which is in string format
There are three different ways to declare a string.
⦁ Single Quotes (‘ ’)
To store the single word into the variable then text that value into single codes(‘Word’)
Example:- Store Fireblaze into the variable X.
X = ‘Fireblaze’
print( X )
⦁ Double Quotes ( “ ” )
To store any sentence into the variable then store the text into the double codes (“Sentence”), why we use double codes because when the sentence contains apostrophe S (‘S) that time the single quotes get an error to store sentence into the variable.
Example:- Store what’s Up Buddy? into the variable X.
X = “What’s Up Buddy?”
print( X )
⦁ Triple Quotes ( ‘‘‘ ’’’ )
To store multiple sentences in the variable then Store text into the triple quotes (‘‘‘Sentence’’’) i.e three single quotes.
Example:- Store Kartik’s friend said that he said great into variable X
X = ‘‘‘ Kartik’s friend said that," he said great " ’’’
print( X )
How to check the data type variable
To check the data type of any variable always use type function it returns the data type of variable which you pass into the function.
Example:
X = ‘FireBlaze’
type( X ) , Ouptut is str (i.e. String)
Access text from string
When you want to access the text from a particular string then the always know the index value of the string. Index value always starts with zero.
Example:- X = ‘ FireBlaze ’, index of this string is 0,1,2,3,4,5,6,7,8,9 Because their are 10 character in the string.
Example To access the text from string:-
Example 1: Suppose access the Fire from variable.
X = ‘FireBlaze’
Syntax: X[ Starting Index : Ending Index + 1]
X = ‘FireBlaze’
x[0:4]
Widget not in any sidebars
Output: Fire
Example 2:-
Access Blaze from variable X = ‘FireBlaze’
Syntax: X[ Starting Index : Ending Index + 1]
X = ‘FireBlaze’
x[4:10]
Output: Blaze
Example 3:-
Access eBlaze start from variable X = ‘FireBlaze’
Syntax: X[ Starting Index: ]
Note:- Here we pass the starting index not mention any ending index because the we start and end till the last point
X = ‘FireBlaze’
x[3:]
Output: eBlaze
Negative Indexing
In this negative indexing part, the index are start from -1 to the end
Example 1:- X = ‘DataScience’, Negative index is = [-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1]
Get the S from the variable X use negative indexing
X = ‘DataScience’ #Negative index is = [-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1]
X[-7] # s is on the position of -7
Output: ‘S’
Example 2: Get the Sci From Variable X use negative indexing
X = ‘DataScience’ #Negative index is = [-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1]
X[-7:-4] # sci is on the position of -7 to -5(But the end position we add -5+1 = -4 )
Output: ‘Sci’
Fancy Indexing
Access the test by using some steps then we use the fancy indexing.
Example:- Access ‘FrBae’ from variable X = ‘FireBlaze’
In this Output, there is a difference between each character is two so we pass the step size in Syntex.
Syntex: X[ Starting Index : Ending Index + 1 : Step Size ]
Step Size:- Difference between character in string
X = ‘FireBlaze’
x[0:10:2] #[ start : stop : step ]
Output: FrBae
Operations On String
Operations on a strings are used to handle the strings text using the built-in functions of python.
The following functions are used to handle the string.
Length Function
It is used to calculate the length of character in string.
Example: check the length of character of variable X = ‘Statistics’
X = ‘statistics’ #store string into variable
len(X) #check the length of character
Widget not in any sidebars
Output: 10
Example 2: Add Space into the character is also calculated as a character.
X = ‘Data Science’ #store string into variable
len(X) #here space is also count as a character
Output: 12
Split Function
It is used to split the string sentence into different parts.
Example 1:
X = 'DataScience Machine Learning'
X.split () # It split the data by space from string(x) i.e each word is separated by space
Output: ‘DataScience’, ‘Machine’, ‘Learning’
Example 2: If you want to split the string by any special character then also use split function but need to pass the special character in split function.
X = 'abc@gmail.com'
X.Split('@') # It split the data by @ by string(X) into part
Output: [‘abc’, ‘gmail.com’]
Lower Function
It is used to convert all your string into lower case.
Example:
X = "Convert All Data Into Lower Case " # The string is in both case upper and lower
print(X.lower()) # With the help of using lower function all string convert into lower case
Output: convert all data into lower case
Upper Function
It Is used to convert all string into upper case
Example:
X = "Convert All Data Into Upper Case" # The string is in both case upper and lower
print(X.upper()) # With the help of using upper function all string convert into Upper case
Output: CONVERT ALL DATA INTO UPPERCASE
Replace Function
It is used to replace any string from the given string.
Example:- Here we replace all the character of D from the B
X = 'Data Science'
print(X.Replace('D', 'B')) # Replace D From B
Output: Bata Science
String and Numeric concatenation
Concatenation means joint two different values together, now in this part string and numerical Concatenation used to join the string and number with the help of using plus (+) operator.
Example:- Store Area = 350, then concrete String ‘Area Of Rectangle is:- ‘ 350
Note:- After Completing the string give comma (,) then use plus + operator
Area = 350 # Numeric value
print( Area of rectangle is :-', + Area)
# Here is a string stored in the quotes and concate area numeric value with using +
# after completing the string give comma (,) then use (+) operator
Output: Area of Rectangle is 350
String To String Concatenation
Now in this part string and string Concatenation used to join this with the help of using plus (+) operator.
Example 1: Create two variables and store string in it and then concrete.
a = 'Python is' # Store string data
b = 'Programming Language' # Store string data
c = a + b # Concate two string to new variable c
c # It Shows the output
Output: ‘Python isProgramming Language’
Example 2:- Add Space between two variable add space in Quotes (“ ”) than concat use ( + )
a = 'Python is' # Store string data
b = 'Programming Language' # Store string data
c = a +" "+b # Concate two string but add space on to quotes " "
c # It Shows the output
Output: ‘Python is Programming Language’
String Format
String formatting is used for concatenation of string and number. In the syntax of format function at the place of number use Curly brackets { } in the string and then apply format function and pass the arguments.
Syntex- ‘String {for number}’.format(pass numeric variable name)
Example 1:
Length = 10
Width = 15
Perimeter = ( 2 * Length + 2 * Width )
Area = Length * Width
Print("Perimeter of Rectangle = {} \nArea of Rectangle = {}".format(Perimeter.Area) )
Output: Perimeter Of Rectangle = 50, Area Of Rectangle = 150
Note:- format function pass numeric value sequentially
Example 2:- Pass value sequentially using the index value of a variable.
Length = 10
Width = 15
Perimeter = ( 2 * Length + 2 * Width )
Area = Length * Width
Print("Perimeter of Rectangle = {} \nArea of Rectangle = {}".format(Perimeter.Area) )
# Here we pass index number of variable in format function to pass value in sequence of string
Output: Perimeter Of Rectangle = 50, Area Of Rectangle = 150
Conclusion
In this blog, you will understand about the strings in python, how to perform operations on a strings in python and how to handle it when the strings is raw and how to concatenate and formatting the strings.