About Lesson
Classroom
Python
Microsoft Windows [Version 10.0.22631.4317]
(c) Microsoft Corporation. All rights reserved.
C:\Users\RAI>py
Python 3.12.3 (tags/v3.12.3:f6650f9, Apr 9 2024, 14:05:25) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> fname='Samar'
>>> mname="Pratap"
>>> lname='''Singh'''
>>> surname="""Khudia"""
>>> fname
'Samar'
>>> mname
'Pratap'
>>> lname
'Singh'
>>> surname
'Khudia'
>>> fullname=f"{fname} {mname} {lname} {surname}"
>>> fullname
'Samar Pratap Singh Khudia'
>>> fname.lane
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'lane'
>>> fname.lname
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'lname'
>>> fname + lname
'SamarSingh'
>>> fullname = fname+ " " + mname + " " + lname + " " + surname
>>> fullname
'Samar Pratap Singh Khudia'
>>> fullname[0]
'S'
>>> fullname[10]
'a'
>>> for ch in fullname:
... print(ch)
...
S
a
m
a
r
P
r
a
t
a
p
S
i
n
g
h
K
h
u
d
i
a
>>> fullname=[1='g'
File "<stdin>", line 1
fullname=[1='g'
^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
>>> fullname[1]='g'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> msg="Ram\nis\ngood\nBoy"
>>> print(msg)
Ram
is
good
Boy
>>> msg="Ram\n\tis\n\t\tgood\n\t\t\tBoy"
>>> print(msg)
Ram
is
good
Boy
>>> fullname
'Samar Pratap Singh Khudia'
>>> fullname[0:5]
'Samar'
>>> 'X' in fullname
False
>>> 'S' in fullname
True
>>> n1=10
>>> n2=20
>>> r=n1+n2
>>> msg="the result is " + r
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>> msg="the result is " + str(r)
>>> msg
'the result is 30'
>>> name="James"
>>> age=50
>>> print("My Name is %s and I am %d year old" %(name,age))
My Name is James and I am 50 year old
>>> print("Hello My name is %s" %name)
Hello My name is James
>>> print("Hello My name is %s" %(name))
Hello My name is James
>>> roi=5.3366325
>>> print("Rate of Ineterest is %.2f" %roi)
Rate of Ineterest is 5.34
>>> n1=100
>>> n2=10
>>> s=n1+n2
>>> print("%d\n%d\n%d" %(n1,n2,s))
100
10
110
>>> print("%5d\n%5d\n%5d" %(n1,n2,s))
100
10
110
>>> print("%5d\n%5d\n_\n%5d" %(n1,n2,s))
100
10
_______
110
>>> print("%5d\n%5d\n-----\n%5d" %(n1,n2,s))
100
10
-----
110
>>> print("%5d\n+%5d\n-----\n%5d" %(n1,n2,s))
100
+ 10
-----
110
>>> print("%5d\n+%4d\n-----\n%5d" %(n1,n2,s))
100
+ 10
-----
110
>>> name
'James'
>>> age
50
>>> msg="My name is {:s} and I am {:d} year old".format(name,age)
>>> msg
'My name is James and I am 50 year old'
>>> a1="Ram"
>>> a1age=20
>>> a2="Sham Kumar"
>>> a2age=30
>>> a3="Anil"
>>> a3age=10
>>> print("{:^10} {:^10}".format("Name","Age"))
Name Age
>>> print("{:^10} {:^10}\n{:^10} {:^10}".format(a1,a1age,a2,a2age))
Ram 20
Sham Kumar 30
>>> print("{:^10} {:^10}\n{:^10} {:^10}\n{:^10} {:^10}".format("Name","Age",a1,a1age,a2,a2age))
Name Age
Ram 20
Sham Kumar 30
>>> print("{:^10} {:^20}\n{:^20} {:^20}\n{:^20} {:^20}".format("Name","Age",a1,a1age,a2,a2age))
Name Age
Ram 20
Sham Kumar 30
>>> print("{:^20} {:^20}\n{:^20} {:^20}\n{:^20} {:^20}".format("Name","Age",a1,a1age,a2,a2age))
Name Age
Ram 20
Sham Kumar 30
>>> print("{:>20} {:^20}\n{:>20} {:^20}\n{:>20} {:^20}".format("Name","Age",a1,a1age,a2,a2age))
Name Age
Ram 20
Sham Kumar 30
>>> print("{:<20} {:^20}\n{:<20} {:^20}\n{:<20} {:^20}".format("Name","Age",a1,a1age,a2,a2age))
Name Age
Ram 20
Sham Kumar 30
>>>