Strings Concepts in Python Part 2

By
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

  • * (multiplication) operator:-It is known as repetition operator.It is used to concatenate the multiple copies of the same string.
e.g.
str1="MNT LAB "
str2="msdotnet "
str3='399 '
print("Repetion of string(str1):\n ",str1*2)
print("Repetion of string(str2): ",str2*4)
print("Repetion of string(str3): ",str3*5)
Output of Above Python program:-
Repetion of string(str1):
  MNT LAB MNT LAB 
Repetion of string(str2):  msdotnet msdotnet msdotnet msdotnet 
Repetion of string(str3):  399 399 399 399 399 
  • % (modulous) :-It is used for string formatting.we use it for formatting purpose in strings.we use for integer =%d, float=%f,char=%c and string=%s etc.
e.g.
str1="Welcome to"
str2="MNT LAB"
str3='M'
str4=124
str5=5.67
print("Value of Integer=%d"%str4)
print('')
print("Value of float=%f"%str5)
print('')
print("Value of string=%s %s"%(str1,str2))
print('')
print("Value of character=%c"%str3)
print('')
print("Hello, %s %s,My Name starts with %c and i earned %d rupees with %f rs per day" %(str1,str2,str3,str4,str5))
Output of Above Python program:-
Value of Integer=124

Value of float=5.670000

Value of string=Welcome to MNT LAB

Value of character=M

Hello, Welcome to MNT LAB,My Name starts with M and i earned 124 rupees with 5.670000 rs per day
  • str.format():- It is an improvement of % formatting in python .We can use .format() function for formatting operations in string.
e.g.
#str.format() is improving of % formatting in python
name="ram"
age='28'
price=2000
print("hello,{} You are {} year old.my salary is rs {}".format(name,age,price))
print("hello,{2} You are {0} year old.my salary is rs {1}".format(age,price,name))
Output of Above Python program:-
hello,ram You are 28 year old.my salary is rs 2000
hello,ram You are 28 year old.my salary is rs 2000
  • r/R(raw data):-It is used for raw string in python.It is used to escape the some special character i.e. ,(coma),' ' (single quotes)," "(double quotes) etc. from the strings.we will discuss it in below examples.
e.g.
print(r"Welcome to msdotnet.co.in/")
print(R'Welcome to youtube channel MNT LAB')
Output of Above Python program:-
Welcome to msdotnet.co.in/
Welcome to youtube channel MNT LAB
  • in (membership Operator):-It is used to return true/false if particular sub-string is present in the specified string or not.If sub-string is present in given string then it return True otherwise False.It is a membership  operator.I have already read it in our previous lecture. 
e.g.
str1="Welcome to"
str2="MNT LAB"
print('to' in str1)
print('Welcome' in str2)
print('lab' in str2) #python is case sensitive
print('MNT' in str2)
Output of Above Python program:-
True
False
False
True
  • not in (membership Operator):- It returns true if a particular sub-string is not match from given string. It is just opposite opposite to 'in' membership operator. It is also known as membership operator in python.
e.g.
str1="Welcome to"
str2="MNT LAB"
print('to'not in str1)
print('Welcome'not in str2)
print('lab'not in str1) #python is case sensitive
print('MNT'not in str2)
print('LAB'not in str1)
Output of Above Python program:-
False
True
True
False
True
String properties:-
There are some properties of python strings as given below:-

1.) All strings objects of built-in type 'str'.
e.g.
str1="Welcome to"
str2=['a','b',1,2,'ram']
str3=(1,2,3,4,5,6)
print(type(str1))
print(type(str2))
print(type(str3))
Output of Above Python program:-
<class 'str'>
<class 'list'>
<class 'tuple'>
2.) Python strings are immutable(unchangeable).
e.g.
str1=
str1="WELCOME"
#str1[0]='M' # error,because string are immutable
#str1[1]'R'  # error,because string are immutable
str1="MNT LAB" # No error,because here str1 is a variable.we can change the variable value
print(str1)
Output of Above Python program:-
MNT LAB
3.) python string can be concatenated using + operator symbol.
e.g.
str1="WELCOME TO "
str2="MNT LAB"
str=str1+str2
print(str)
Output of Above Python program:-
WELCOME TO MNT LAB
4.) python strings can be replicated during printing as given below:-
e.g.
str1="WELCOME "
str2="MNT "
print(str1*5)
print(str2*4)
print('* '*7)
print('/n '*6)
print('- '*8)
Output of Above Python program:-
WELCOME WELCOME WELCOME WELCOME WELCOME 
MNT MNT MNT MNT 
* * * * * * * 
/n /n /n /n /n /n 
- - - - - - - - 
Special Notes:-
datetime Module in Python:- datetime is a module of python,To use this module we can display the dates and times easily. We will learn each concepts with an example.
1.) Get Current date and time:- We can get current system date and time by below python program :-
e.g.

import datetime
t1=datetime.datetime.now()
print("System current date and time: ",t1)
print("System current year: ",t1.year)
print("System current day: ",t1.day)
print("System current month: ",t1.month)
Output of Above Python program:-
System current date and time:  2020-05-05 22:52:29.259476
System current year:  2020
System current day:  5
System current month:  5
2.) Get Current System date:-We can get current system date by below python codes :-
e.g.
import datetime
t1=datetime.datetime.now()
#buit-in method is already avaible in datatime module
print("System current date: ",t1.date)
print(" ")
t2=datetime.date.today()
t3=t2.year
print("System current date: ",t2)
print("System current year: ",t3)
Output:-
System current date:  <built-in method date of datetime.datetime object at 0x00000250139B1CF0>

System current date:  2020-05-05
System current year:  2020
3.) Create Date object:-We can create date object by following way in python.
e.g.

#create a date object
import datetime
d1=datetime.date(2020,5,6)
d2=datetime.date(2019,8,3)
#d3=datetime.date(2018,05,06)#get error because double digit is used in month and day
print("Date is: ",d1)
print("Date is: ",d2)
#print("Date is: ",d3)
Output:-
Date is:  2020-05-06
Date is:  2019-08-03
For More...
  1. Python Keywords with examples
  2. Some built-in types of python
  3. Multiple variable assignment in python
  4. Indentation concepts in python language
  5. Arithmetic operations and compound operations in python
  6. Type conversions in python with examples
  7. How to use pow() function in python
  8. Create setup file for windows operation systems
Watch complete Lecture Video:-

0 comments:

Post a Comment

Powered by Blogger.