Python practice 04/08/2024
String related functions:
s = "hello world"
len(s)
11
[Link]('o')
2
[Link]('o' , 2 , 5)
1
s = "hello world hello world"
[Link]('wo')
2
[Link]('hello')
2
[Link]('o')
4
[Link]('p')
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
[Link]('p')
ValueError: substring not found
[Link]('o')
4
[Link]('p')
-1
[Link]
<built-in method title of str object at 0x000002A8B80165F0>
[Link]()
'Hello World Hello World'
[Link]()
'hello world hello world'
[Link]()
'HELLO WORLD HELLO WORLD'
[Link]()
'Hello world hello world'
[Link]()
False
[Link]()
True
[Link]('o', 'a')
'hella warld hella warld'
[Link]()
False
'*'.join(s)
'h*e*l*l*o* *w*o*r*l*d* *h*e*l*l*o* *w*o*r*l*d'
'###'.join(s)
'h###e###l###l###o### ###w###o###r###l###d### ###h###e###l###l###o###
###w###o###r###l###d'
[Link]()
['hello', 'world', 'hello', 'world']
[Link]('o')
['hell', ' w', 'rld hell', ' w', 'rld']
[Link]('o')
('hell', 'o', ' world hello world')
s1 = 2234
s2 = '12345'
s3 = 'hello123'
s4 = ' '
[Link]()
Traceback (most recent call last):
File "<pyshell#35>", line 1, in <module>
[Link]()
AttributeError: 'int' object has no attribute 'isdigit'
[Link]()
True
[Link]()
False
s5 = 'h123'
[Link]()
False
[Link]()
False
[Link]()
True
[Link]()
True
[Link]()
False
[Link]()
False
l ='hello'
[Link]()
True
l1 = ' hello python '
[Link]()
'hello python '
[Link]()
' hello python'
[Link]()
'hello python'
x = 'abcd'
[Link]('cd')
False
[Link]('ab')
True
[Link]('cd')
True
PROGRAM 1: write a program that reads a line and prints
- Number of uppercase letters
- Number of lowercase letters
- Number of alphabet letters
- Number of digits
E.g. random thing will give these inputs
PROGRAM 2: write a program that reads a string and then prints a string that
capitalizes every other letter in the string. E.g. school becomes sChOoL (done)
PROGRAM 3: write a program that reads a string and checks whether the string is
a palindrome or not. E.g. mom; madam; dad, etc.
Row
Row
Row
Row
Column column column column
column1 column2` column3sa
For row in range (4):#outer loop for row
For col in range(3):#inner loop for cols
print(row) # loop variable
*
**
***
****
4321
432
43
4
1
11
111
1111
5 1
5 3
9 1
9 3
9 5
9 7
Number system:
Decimal
Radix or base 10
Digits (0-9)
Binary
Base or radix 2
Digits (0,1)
Hexadecimal
Base 16
Digits
0-9, A-F
A=10,B=11,C=12,D=13,E=14,F=15
Octal
Base 8
Digit (0-7)
Python numbers
a=456 #Decimal
b=0o23 #octal 0o
h=0xFF #hexadecimal 0x
b=0b0111 #binary representation 0b
13 - binary
13/2 = 6 (r 1)
6 / 2 = 3 ( r 0)
3/2 = 1 (r 1)
1 / 2 = 0 (r 1)
Number: 1011 (in binary)
101 binary=2**0*1+2**1*0+2**2*1=1+0+4=5
HOMEWORK
13//2=6 r=1
6//2=3 r=0
3//2 r=1
1//2 r=1
0
b=b+r*i
Or
b+=r*i
b=0
r=0
i=1
Ist iteration
0+1*1
b=1
i=10
2nd
1+0*10
1
b=1
i=100
3rd
1+1*100
b=101
i=100*10
i=1000
4th
101+1*1000
101+1000
Binary number 1101