String Practice
String Practice
May 2, 2023
[ ]: a=65
b=54
c=84
# Find the min number
[5]: a=65
b=54
c=84
print('a is min') if a<b and a<c else print('b is min') if b<a and b<c else␣
↪print('c is min')
b is min
[6]: 30>5
[6]: True
[7]: True
print(a) if a>b and a>c else print(b) if b>a and b>c else print(c)
print(a, 'is max') if a>b and a>c else print(b, 'is max') if b>a and b>c else␣
↪print(c, 'is max')
a- 65456463541563456
b- 6546541654896468
1
c- 54685468484684684
65456463541563456 is max
25
[19]: a='ladies'
b='and gentlemen'
print(a+' ' +b)
[31]: a='squirrel'
print(len(a))
print(a[0])
print(a[-8])
print(a[-len(a)])
print(a[len(a)-1])
print(a[7])
print(a[-1])
8
s
s
s
l
l
l
[18]: a='squirrel'
print(len(a))
print(a[0:1])
print(a[0:2])
print(a[0:3:1])
print(a[0:4:1])
2
print(a[0:5])
print(a[0:6:1])
print(a[0:8:1])
print(a[len(a)-2])
print(a[len(a)-1:len(a)-2:-1])
8
s
sq
squ
squi
squir
squirr
squirrel
e
l
[40]: a='squirrel'
print(a[-1:-2:-1])
print(a[-1:-3:-1])
print(a[-1:-4:-1])
print(a[-1:-5:-1])
print(a[-1:-6:-1])
print(a[-1:-7:-1])
print(a[-1:-8:-1])
print(a[-1:-9:-1])
l
le
ler
lerr
lerri
lerriu
lerriuq
lerriuqs
[46]: a='squirrel'
print(a[2:6])
print(a[-4::1])
uirr
rrel
3
print(a[1::])
print(a[2::])
print(a[3::])
print(a[4::])
print(a[5::])
print(a[7::])
print(a[8::])
print(a[9::])
print(a[10::])
Hello Python
Hello Python
Hello Python
ello Python
llo Python
lo Python
o Python
Python
ython
thon
hon
on
1
1
[1]: a=['this','is','python','class']
print(str(a))
[15]: id("Although that way may not be obvious at first unless you're Dutch.")
id("Although that way may not be obvious at first unless you're Dutch.")
[15]: 2946197705008
[5]: my_str="Although that way may not be obvious at first unless you're Dutch."
my_str1="Although that way may not be obvious at first unless you're Dutch."
print(id(my_str))
print(id(my_str1))
type(my_str)
len(my_str)
2607340340528
4
2607340339888
[5]: 66
[3]: a='Learnbay'
print(a*8)
LearnbayLearnbayLearnbayLearnbayLearnbayLearnbayLearnbayLearnbay
True
65 97
[8]: print(chr(65))
[10]: b='65'
print(chr(b))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_13392\3714565741.py in <module>
1 b='65'
----> 2 print(chr(b))
5
my name is Gunjan Acharya
My Name Is Gunjan Acharya
My mail id is [email protected]
mY MAIL ID IS [email protected]
6
3
5
6
[15]: a='My mail id is [email protected]'
print(a.count('gmail'))
31
39
29
8
33
33
39
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_16380\4117723427.py in <module>
1 a='My mail id is [email protected]'
2 print(a.split())
----> 3 print(a.splilines())
4 print(type(a.split()))
7
['Hello everyone!', 'This is Python class.', 'We are learning strings.', 'Thank
you!']
[39]: lst=['Hello','This','is','not','Java','class']
print(' '.join(lst))
[4]: a='&Drink&'
print(a.strip('&'))
Drink
[4]: b='*******Drink****'
print(b.strip('*'))
print(b.rstrip('*'))
print(b.lstrip('*'))
Drink
*******Drink
Drink****
[8]: c='&*%^*&^*&Hello*^^&%&^%&^'
print(c.strip('&*%^'))
Hello
[17]: d='Hello'
print(d.ljust(len(d)+2,'+'))
Hello++
Gunjan Acharya**********
**********Gunjan Acharya
********Gunjan Acharya*******
++++++++++Gunjan Acharya----------
[28]: my_str='Python'
# Output- Python++
my_str.ljust(len(my_str)+2,'+')
[28]: 'Python++'
8
[38]: a='Python12354$#%^$'
b='Gunjan Acharya'
print(a.isalnum())
print(b.isalnum())
False
False
False
[20]: a='_534'
print(a)
ad=70
print(ad)
print(a.isidentifier())
_534
70
True
[21]: a='Hello\nPython'
print(a.isprintable())
print(a)
print(a.isnumeric())
False
Hello
Python
False
[1]: 'This is
Python
class'
[4]: my_str="Although that way may not be obvious at first unless you're Dutch."
my_str1="Although that way may not be obvious at first unless you're Dutch."
print(id(my_str))
print(id(my_str1))
9
1763927262896
1763927263024
[5]: my_str=5
my_str1=5
print(id(my_str))
print(id(my_str1))
1763843926448
1763843926448
print(my_str.find('is',3))
[37]: ord('A')
[37]: 65
22
17
17
s = chr(27) + chr(97)
print(s.isprintable())
True
False
[6]: chr(27)
[6]: '\x1b'
[7]: chr(97)
[7]: 'a'
[8]: chr(65)
[8]: 'A'
10
[11]: s='\x1b'+'a'
print(s.isprintable())
False
[16]: print(ord('A'),ord('Z'),ord('a'),ord('z'))
65 90 97 122
Enter a String- 5
5 <class 'str'>
[22]: b=eval('a')
print(b,type(b))
5 <class 'str'>
[1]: a='python'
a[0:7]
[1]: 'python'
[1]: my_str="hello"
print(my_str[:2])
he
[2]: trunc()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_15264\432065784.py in <module>
----> 1 trunc()
[3]: trunc()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_15264\432065784.py in <module>
----> 1 trunc()
11
[5]: ('hello'+'2')*2
[5]: 'hello2hello2'
[16]: print()
[2]: print('tom\ndick\nharry')
tom
dick
harry
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_1620\2071935458.py in <module>
----> 1 apple = mango
[4]: round(46.8)
[4]: 47
hello-how-areyou
12
[7]: print('hello-' + 'how-are-you')
hello-how-are-you
[ ]:
13