0% found this document useful (0 votes)
84 views

String Practice

The document contains code snippets demonstrating various string methods in Python like capitalize(), lower(), upper(), strip(), split(), join(), isalpha(), isdigit() etc. It shows how to manipulate, check properties and format strings. Examples include changing case, stripping characters, splitting and joining strings, checking string properties and formatting strings by padding them.

Uploaded by

TheAncient01
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

String Practice

The document contains code snippets demonstrating various string methods in Python like capitalize(), lower(), upper(), strip(), split(), join(), isalpha(), isdigit() etc. It shows how to manipulate, check properties and format strings. Examples include changing case, stripping characters, splitting and joining strings, checking string properties and formatting strings by padding them.

Uploaded by

TheAncient01
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

String_practice

May 2, 2023

a=53 print(‘a is even’) if a%2==0 else print(‘a is odd’)

[ ]: 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]: 30>5 and 30>2

[7]: True

[ ]: a=int(input('Enter a number- '))


b=int(input('Enter a number- '))
c=int(input('Enter a number- '))
# Find the maximum number

print(a) if a>b and a>c else print(b) if b>a and b>c else print(c)

[9]: a=int(input('a- '))


b=int(input('b- '))
c=int(input('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

[10]: a='raise the black flag lads'


print(len(a))

25

[19]: a='ladies'
b='and gentlemen'
print(a+' ' +b)

ladies and gentlemen

[26]: a='my name is gunjan acharya'*6


print(a)

my name is gunjan acharyamy name is gunjan acharyamy name is gunjan acharyamy


name is gunjan acharyamy name is gunjan acharyamy name is gunjan acharya

[31]: a='squirrel'

# Get the first element

print(len(a))
print(a[0])
print(a[-8])
print(a[-len(a)])

# Get the last element

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

[58]: a='Hello Python'


print(a[0:len(a)])
print(a[-len(a)::1])
print(a[0::])

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

[19]: a='Hello Python'


print(a.count('He'))
print(a.count('e'))

1
1

[1]: a=['this','is','python','class']
print(str(a))

['this', 'is', 'python', 'class']

[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

[5]: str1 = 'Python'


str2 = 'Python'
print(str1 is str2)

True

[6]: str1 = 'A'


str2 = 'a'
print(ord(str1),ord(str2))

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))

TypeError: an integer is required (got type str)

[1]: a='my name is Gunjan Acharya'


print(a)
print(a.capitalize())
print(a)

my name is Gunjan Acharya


My name is gunjan acharya
my name is Gunjan Acharya

[2]: a='my name is Gunjan Acharya'


print(a)
print(a.title())

5
my name is Gunjan Acharya
My Name Is Gunjan Acharya

[3]: a='my name is Gunjan Acharya'


print(a)
print(a.upper())

my name is Gunjan Acharya


MY NAME IS GUNJAN ACHARYA

[4]: b='MY NAME IS GUNJAN ACHARYA'


print(b)
print(b.lower())

MY NAME IS GUNJAN ACHARYA


my name is gunjan acharya

[5]: a='my name is Gunjan Acharya'


print(a)
print(a.swapcase())

my name is Gunjan Acharya


MY NAME IS gUNJAN aCHARYA

[6]: c='My mail id is [email protected]'


print(c)
print(c.swapcase())

My mail id is [email protected]
mY MAIL ID IS [email protected]

[7]: a='my name is Gunjan Acharya'


b='MY NAME IS GUNJAN ACHARYA'
c='My mail id is [email protected]'
print(a.casefold())
print(b.casefold())
print(c.casefold())

my name is gunjan acharya


my name is gunjan acharya
my mail id is [email protected]

[12]: a='My mail id is [email protected]'


print(a.count('a'))
print(a.count('m'))
print(a.count('a',5,len(a)))

6
3
5

6
[15]: a='My mail id is [email protected]'
print(a.count('gmail'))

[10]: a='My mail id is [email protected]'


print(a.index('m',4,39))
print(len(a))
print(a.index('@'))

31
39
29

[22]: a='My mail id is [email protected]'


print(a.find('i',6,39))
print(a.find('i',28,39))
print(a.rfind('i',6,39))
print(len(a))

8
33
33
39

[36]: a='My mail id is [email protected]'


print(a.split())
print(a.splilines())
print(type(a.split()))

['My', 'mail', 'id', 'is', '[email protected]']

---------------------------------------------------------------------------
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()))

AttributeError: 'str' object has no attribute 'splilines'

[34]: m='''Hello everyone!


This is Python class.
We are learning strings.
Thank you!'''
print(m.splitlines())

7
['Hello everyone!', 'This is Python class.', 'We are learning strings.', 'Thank
you!']

[39]: lst=['Hello','This','is','not','Java','class']
print(' '.join(lst))

Hello This is not Java class

[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++

[33]: e='Gunjan Acharya'


print(e.ljust(len(e)+10,'*'))
print(e.rjust(len(e)+10,'*'))
print(e.center(len(e)+15,'*'))
print(e.ljust(len(e)+10,'-').rjust(len(e)+10+10,'+'))

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

[41]: c='Raise the black flag lads'


print(c.isalpha())

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'

File "C:\Users\mark4\AppData\Local\Temp\ipykernel_13928\2399092632.py", line 1


'This is
^
SyntaxError: EOL while scanning string literal

[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

[24]: my_str='This is s Python i class'

print(my_str.find('is',3))

[37]: ord('A')

[37]: 65

[47]: my_str='This h is Python class'


print(len(my_str))
print(my_str.index('c',0,18))
print(my_str.find('c'))

22
17
17

[5]: s = chr(65) + chr(97)


print(s.isprintable())

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

[18]: a=input('Enter a String- ')


print(a,type(a))

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()

NameError: name 'trunc' is not defined

[3]: trunc()

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_15264\432065784.py in <module>
----> 1 trunc()

NameError: name 'trunc' is not defined

11
[5]: ('hello'+'2')*2

[5]: 'hello2hello2'

[16]: print()

File "C:\Users\mark4\AppData\Local\Temp\ipykernel_15264\3235145164.py", line 1


printat's okay'")
^
SyntaxError: invalid syntax

[1]: print('"Once upon a time…", she said.')


print("He said, 'Yes!'")
print('3\')
print("'That's okay'")

File "C:\Users\mark4\AppData\Local\Temp\ipykernel_1620\3211825021.py", line 3


print('3\')
^
SyntaxError: EOL while scanning string literal

[2]: print('tom\ndick\nharry')

tom
dick
harry

[3]: apple = mango

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_1620\2071935458.py in <module>
----> 1 apple = mango

NameError: name 'mango' is not defined

[4]: round(46.8)

[4]: 47

[9]: print('hello' + '-' + 'how' + '-' + 'are' + 'you')

hello-how-areyou

12
[7]: print('hello-' + 'how-are-you')

hello-how-are-you

[ ]:

13

You might also like