In this tutorial, we will learn about String handling in Python programming language
In python language, the string is a set of character set in a particular order. The string can have more than one character and space.
we can manipulate the string in many ways, using the single quote, double quote and triple quote.
Python allows string manipulation of single quote and double quotes in the same way. However, manipulation with triple single quotes and triple double quotes differ.
Know more about String manipulation String methods in Python language
An empty string is a string that has zero characters.
Example
Program 1
1.print('Hello world') //print using with single quote 2. print("Hello world") //print using with double quotes 3. print('''Hello world''') //print using with triple-single quotes 4. print("""Hello world""") //print using with triple-single quotes
Output here
>>> >>> print('Hello world') #String with single quotes Hello world >>> print("Hello world") #String with double quotes Hello world >>> print('''Hello world''') #String with triple-single quotes Hello world >>> print("""Hello world""") #String with triple-double quotes Hello world >>>
Single quotes are used to assign the string to a variable.
Example
string_1=’Hello world’ //assign a string to the variable using single quotes
print (string_1) //print string using variable
Double quotes are used to assign the string to a variable.
strint_2=”Hello Python” //assign a string to the variable using double quotes
print (string_2) //print string using variable
Triple single quotes are used to assign the string to a variable.
strint_3=”’Hello Python”’ //assign a string to the variable using triple -single quotes
print (string_3) //print string using variable
Triple double quotes are used to assign the string to a variable.
strint_4=”””Hello Python””” //assign a string to the variable using triple -double quotes
print (string_4) //print string using variable
Example
program and out put using Python shell
>>> >>> String_1='Hello' >>> print String_1 Hello >>> >>> String_2="Hello" >>> print String_2 Hello >>> >>> String_3='''Hello''' >>> print String_3 Hello >>> >>> String_4="""Hello""" >>> print String_4 Hello >>> >>>
We can use the triple quote to print multiple line string
Strinng_1='''multiple line string'''
2.Print multiple line string using triple single quotes
Strinng_2="""multiple line string"""
Example
String_1='''Hello Python Language''' print String_1 String_2="""Python is a best Language""" print String_2
Output
Hello Python Language Python is a best Language
We can join two or more string using concatenation of string in Python.
To join more than one string, (+) operator is used.
String concatenation using Python shell
>>> >>> #String concatenation using single quotes >>> 'Hello'+'World' 'HelloWorld' >>> >>> #String concatenation using double quotes >>> "Hello"+"World" 'HelloWorld' >>> >>> #String concatenation with space >>> 'Python'+' '+'Programming'+' '+'Language' 'Python Programming Language' >>> >>>
concatenation two or more string using variable
String_1='Hello' #The string is assigned to the variable 1 String_2='Pyton' #The string is assigned to the variable 2 #Two String Concatenation using (+) operator print("String_1+String_2 :",String_1+ String_2) String_3='Python' #The string is assigned to the variable 3 String_4='language' #The string is assigned to the variable 4 #Two String Concatenation using (+) operator print("String_3+space+String_4 :",String_3+" "+String_4)
Output
(‘String_1+String_2 :’, ‘HelloPyton’)
(‘String_3+space+String_4 :’, ‘Python language’)
More other concatenation methods in Python
>>> #concatenate two String using single quotes >>> 'Hello''World' 'HelloWorld' >>> >>> #concatenate two String using double quotes >>> "Hello""World" 'HelloWorld' >>> >>> #concatenate two String using single quotes with space >>> 'Hello'" "'World' 'Hello World' >>> >>> #concatenate two String using double quotes with space >>> "Hello"" ""World" 'Hello World' >>>
We can create multiple strings in python language
The multiply (*) operator is used for this operation
>>> >>> #this is a string >>> 'Python' 'Python' >>> #multiplication of the string >>> 'Python' * 10 'PythonPythonPythonPythonPythonPythonPythonPythonPythonPython' >>> >>> #multiplication of the string >>> ('Python'+' ') * 10 'Python Python Python Python Python Python Python Python Python Python ' >>>
>>> >>> Word="Hello world" >>> letter=Word[0] >>> print letter H >>> letter=Word[2] >>> print letter l >>>
Suggested for you
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python program to calculate the sum of odd and even numbers in a list In…
Python code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…