There are some operational functions for the strings as given below:-
1.) split() :-The split() function is used to split(breaks up) at a specified separator and returns a list of strings.
Syntax
string.split(separator,maxsplit)
The split function takes maximum two parameters as given below:-
Separator:-This is an optional parameter.If separator is specified ,the string split at this specified separator.If separator is not specified ,the string is split at white space (space,newline,coma etc.) separator.
maxsplit:-This is also an optional parameter.The maxsplit is used to define maximum number of splits. The default value of max split is -1 ,means no limit of number of splits.
There are some search and replace functions operations in strings as given below:- 1.) find():- The find() function returns the index value of the string where the substring is found between start index and end index.start index and end index is optional parameter.
If substring is present in given string then this function returns the index of the first occurrence of the substring.
If the substring is not exist inside the string then it returns -1 index
String operations in python: There are many Built-in string function available in python language.Here We will learn content test functions with examples as given below:- 1.) isalpha() :-It returns True ,if all characters in the string are alphabets otherwise False. The alphabets can be lower case and upper case both.This function doesn't take any parameters.If string contains a white space or any numeric value then this function returns False. Syntax:- String.isalpha()
String operators in Python:-There are some operators used to perform some basic operations on strings.
+ (plus) operator):-it is known as concatenation operator.it is used to concatenate two or more strings in python language.
e.g.
str1="Welcome to"
str2="MNT LAB"
str3="msdotnet"
str=str1+str2
print("Concatenate the two string: ",str)
print("Concatenate the two string: ",str1+' '+str3)
Output of Above Python program Concatenate the two string: Welcome toMNT LAB Concatenate the two string: Welcome to msdotnet
Operators are special symbols in python which are responsible for a particular operation between two or more operands.Operators are more helpful for doing operations between two operands.We will learn everything about different type of operators in python with examples. e.g.
a=5;b=6;
c=a+b
print("value of a+b= ",c)
Output of above program:-
value of a+b= 11 Descriptions:- In above program operators --> =,+ operands --> a,b,c There are many variety of operators present in python as follows:-
The print() function is used for sending the output to the screen.It is used to display the output of any variables like string,list,tuple,set,dictionary,etc.Print() function is a built-in function in python language. Syntax:- print(*objects,sep=' ',end='\n',file=sys.stdout,flush=False) Descriptions:- 1.)objects:-The object can be Integer,Float,string,list,tuple,set,etc. e.g.
a=234
b='ram'
list=[1,2,3,4,5,6]
tuple=(1,2,3,4,5,6)
dictionary={'a':'apple','b':'bag','c':'cat','d':'dog'}
set={'a','e','i','o','u'}
print("value of a=",a)
print("value of b=",b)
print("value of list=",list)
print("value of tuple=",tuple)
print("value of dictionary=",dictionary)
print("value of set=",set)
print("value of 12*45=",12*45)
output of the above python program:-
value of a= 234
value of b= ram
value of list= [1, 2, 3, 4, 5, 6]
value of tuple= (1, 2, 3, 4, 5, 6)
value of dictionary= {'a': 'apple', 'b': 'bag', 'c': 'cat', 'd': 'dog'}
value of set= {'o', 'e', 'i', 'a', 'u'}
value of 12*45= 540
2.)sep=' ' :-This parameter is used to give space between multiple objects.You an use this parameter other than space also.It will be shown in below programs.
e.g.
a=5
b=6
c=24
d=60
e='ram'
f='sita'
print("value of a,b,c,d=",a,b,c,d)
print("value of a,b,c,d=",a,b,c,d,sep=' ')
print("value of a,b,c,d=",a,b,c,d,sep=',')
print("value of a,b,c,d=",a,b,c,d,sep='\n')
print("value of e,f =",e+' '+f)
output of the above python program:- value of a,b,c,d= 5 6 24 60 value of a,b,c,d= 5 6 24 60 value of a,b,c,d=,5,6,24,60 value of a,b,c,d= 5 6 24 60 value of e,f = ram sita 3.)end=' \n' :-It means,when we call print () function each time.it will end at a new line.In python by default ,print() function always ends at a new line.You can change it. i will see in the programs.
Literal is defined as a raw data that is given in a variable or constant. There are various types of Literals in python as given below:-
Numeric Literals
String Literals
Boolean Literals
Special Literals
Collections Literals
1.)Numeric Literals:- Numeric Literals are immutable(unchangeable).There are three different numerical types integer, Float and complex present in Numeric Literals.
e.g.
Integer Literals:-
a=0b1111 #binary Literal
b=400 #decimal Literal
c=0o62 #octal Literal
d=0x66 #hexadecimal Literal
print("value of a=",a)
print("value of b=",b)
print("value of c=",c)
print("value of d=",d)
Output of above Python codes:-
value of a= 15 value of b= 400 value of c= 50 value of d= 102
Note:-
In above program .I have assigned different literals(binary,decimal,octal and hexadecimal) in different variables(a,b,c,d).
When we run the program,all literal values convert into decimal values.
e.g.
Float Literals:-Real numbers with integer and fractional part.
float_a=20.12
float_b=26e2
float_c=12.43
print("value of float_a=",float_a)
print("value of float_b=",float_b)
print("value of float_c=",float_c)
Output of above Python codes:-
value of float_a= 20.12 value of float_b= 2600.0 value of float_c= 12.43
e.g
Complex Number:
c=3+12.25j
print('complex number c=',c)
print('complex number real value=',c.real)
print('complex number imaginary value=',c.imag)
Output of above Python codes:-
complex number c= (3+12.25j) complex number real value= 3.0 complex number imaginary value= 12.25
2.) String Literals:- A string Literal can be generated by enclosing the text in the quotes.we can use both quotes(single,double as well as triple) for a string.
e.g.
a='i am a student' # string literals
b="hello friends !"
c='''How are you'''
d='c' #character literal
e="""welcome #multi line string
To
My website:
https://www.msdotnet.co.in """
f= r"raw''\nvalues" #raw string
g=u"\u00dcnic\u00f6de" #unicode string
print('value of a=',a)
print('value of b=',b)
print('value of c=',c)
print('value of d=',d)
print('value of e=',e)
print('value of f=',f)
print('value of g=',g)
Output of above Python codes:- value of a= i am a student value of b= hello friends ! value of c= How are you value of d= c value of e= welcome #multi line string To My website https://www.msdotnet.co.in value of f= raw''\nvalues value of g= Ünicöde
3.) Boolean Literals:-A Boolean literals have only two values:true or false.
a=(True==1)
b=(False==0)
c=(True+10)
d=(False+20)
e=(False==1)
print('value of a=',a)
print('value of b=',b)
print('value of c=',c)
print('value of d=',d)
print('value of e=',e)
Output of above Python codes:-
value of a= True value of b= True value of c= 11 value of d= 20 value of e= False
4.) Special Literals :- Python contains one special literal i.e. None. we use this literal(None) that field is not created.
5.) Collection Literals:-There are four different types of collection literal :
List
Tuple
Sets
Dictionary
List,Tuple,set,dictionary literals:-List contains items of different data types.list are mutable(changeable).
e.g.
0 1 2 3 4 5
list_1=['ram','sita',200,35.45,105,0b101] # list contains different types of values
#we can retrieve list value using slice operator([] and [:])
list_2=list_1[0:3]
list_3=list_1[2:5]
list_4=[]
# + sign is used to concatenation(add) two list and * sign is used for repetition .
list_a=['ram','20','12.32']
list_b=['sita','mohan',500]
list_c=(list_a)+(list_b)
list_d=(list_a)*2
list_e=(list_b)*2
tuple=(1,2,3,4,5,6)
dictionary={'a':'apple','b':'bag','c':'cat','d':'dog'}
set={'a','e','i','o','u'}
print(list_1)
print(list_2)
print(list_3)
print(list_4)
print(list_c)
print(list_d)
print(list_d)
print(tuple)
print(dictionary)
print(set)
Special Notes:- Input () built-in function:- This function(or method) reads a line from console input,converts into a string and return it. This function is more important in python to read a input line from console(prompt). e.g.
a=int(input("Enter the First value:"))
b=int(input("Enter the second value:"))
sum=a+b
multi=a*b
print("your sum of a and b =",sum)
print("your multi of a and b =",multi)
c=input("Enter Your name: ")
print("my name is= ",c)
print("Your best friend's name: ")
d=input()
print("my friend's name="+d)
Output of above Python codes:-
Enter the First value:10 Enter the second value:20 your sum of a and b = 30 your multi of a and b = 200 Enter Your name: RAM my name is= RAM Your best friend's name: MOHAN my friend's name=MOHAN Watch Complete Lecture Video:-