Python
Python
String Formatting
Ex:-print("Hello {} Your Age is {}".format(name,age+2)) ==>Python 3({} are called
placeholders)
print(f"Hello {name} Your Age is {age+2}")==>Python 3.6
String Indexing
Ex:-name="Himanshu"
print(name[-8]) Output:-H
String Slicing/Substring
Ex:-lang="python"
print(lang[0:2])==>py
print(lang[-3:5])==>hon
print(lang[1:])==>ython
print(lang[:])==>python
print(lang[:2])==>py
Step Argument
Ex:- print("Himanshu"[0:6:1])==>Himansh
print("Himanshu"[0:6:2])==>Hmnh
print("Himanshu"[:6:3])==>Hah
print("Himanshu"[6::-1)==>hsnamiH
print("Himanshu"[::-1]) or print("Himanshu"[-1::-1])==>uhsnamiH(Or trick to
reverse a string)ion
String Methods
1.len() function
Syntax:-len(st)==>It will return length of st
print(len(st))==>It will print length of st
2.lower() method
Ex:-
t="hImaNsHu pAtHak"
print(t.lower())==>himanshu pathak
3.upper() method
Ex:-
t="hImaNsHu pAtHak"
print(t.upper())==>HIMANSHU PATHAK
4.title() method
Ex:-
t="hImaNsHu pAtHak"
print(t.title())==>Himanshu Pathak
5.count() method
Ex:-
t="hImaNsHu pAtHak"
print(t.count("H"))==>2
6.strip() method
Ex:-
t=" Himanshu "
print(t.strip())==>"Himanshu"
print(t.lstrip())==>"Himanshu "
print(t.rstrip())==>" Himanshu"
7.replace() method
Ex:-
t="She is beautiful and she is a good dancer"
print(t.replace(" ","_"))==>"She_is_beautiful_and_she_is_a_good_dancer"
print(t.replace(" ","_",2))==>She_is_beautiful and she is a good dancer
print(t.replace("is","was",1))==>"She was beautiful and she is a good dancer"
print(t.replace("is","was",2))==>"She was beautiful and she was a good dancer"
print(t.replace("is","was"))==>"She was beautiful and she is a good dancer"
8.find() method
Ex:-
t="She is beautiful and she is a good dancer"
print(t.find("is"))==>4
print(t.fnd("is",5))==>25(It will start finding from fifth index)
9.center() method
Ex:-
t="Himanshu"
print(t.center(10,"*"))==>"*Himanshu*"(Here 10 is the total length after adding
*(string) to both sides)
print(t.center(12,"*"))==>"**Himanshu**"
print(t.center(9,"*"))==>"*Himanshu"
print(t.center(11,"*"))==>"**Himanshu*"
Assignment Operator
name+="it"
age+=1
If statement:-
if age>=14:
\\space is necessary after if
print("You are above 14") \\Here the statements which are having tab space
at starting are inside if statement
Ex:-
x=18
if x==18:
pass \\It does not show any output
if else statement:-
Ex:-
age=int(input("Enter your age:-"))
if age>=14:
else:
import random
w=random.randint(0,10)
g=int(input("Guess:-"))
if w==g:
print("You Win!!")
print(w)
else:
if w>g:
print("too low")
print(w)
else:
print("too high")
print(w)
Ex:-1.
age=19
name="himanshu"
print("TRUe")
else:
print("False")
2.
name,age=input("Enter name and then age:-").split()
age=int(age)
else:
print("You cant")
if-elif-else statement:-
Ex:-
age=int(input("Enter your age:-"))
if 0<age<=3:
price="free"
elif 3<age<=10:
price=150
elif 10<age<=60:
price=250
else:
price=200
print("Price is {}".format(price))
in Keyword:-
The in keyword has two purposes:
Ex:-
1.For strings
name="Himanshu"
if "H" in name:
print("Present")
else:
print("Not present")
2.For lists
fruits = ["apple", "banana", "cherry"]
if "banana" in fruits:
print("yes")
for x in fruits:
print(x)
if name:
else:
while loop:-
Ex:-
1.To print hello world 10 times
i=0
while i<10:
print("hello world")
i+=1
2.Sum of Numbers
i=1
sum=0
while i<=10:
sum+=i
i+=1
print(sum)
i=0
sum=0
while i<n1:
sum+=int(n[i])
i+=1
print(sum)
i=0
temp=""
while i<len(name):
temp+=name[i]
print("{} : {}".format(name[i],name.count(name[i])))
i+=1
Infinite Loop
while True:
print("hello")
For loop:-
Ex:-print hello world 10 times(Here range(10) means i ki value 0 se 9 tak jaayegi)
for i in range(10):
print("Hello World")
Ex:-2
for i in range(1,11): \\Now it will run for i=1(starting) to i=11-1=10(ending)
print("hello world {}".format(i))
for i in range(11):
sum+=i
print(sum)
n=input("Enter a Number:-")
sum=0
for i in range(len(n)):
sum+=int(n[i])
print(sum)
for i in range(10):
if i==5:
break
print(i)
for i in range(10):
if i==5:
break
print(i)
Ex:-1
for i in input("Enter name:-"):
print(i)
sum+=int(i)
print(sum)