Saturday, November 2, 2019

split method in python


example 1:
split method is used for to split the given string in the left to right direction

s="anil durgam embedded"
l=s.split()
print(l)

output:
['anil', 'durgam', 'embedded']

example2:

s="anil durgam embedded"
l=s.split()
print(l)
for x in l:
    print(x)

output:
['anil', 'durgam', 'embedded']
anil
durgam
embedded


example 4:

s="03-11-2019"
l=s.split("-")
print(l)
for x in l:
    print(x)

output:
['03', '11', '2019']
03
11
2019


example 5:

s="anil durgam embedded developer india"
l=s.split(" ",3)
print(l)
for x in l:
    print(x)

output:
anil
durgam
embedded
developer india

example 6:
s="10 20 30 40 50 60 70 80"
l=s.split(" ",3)
print(l)
for x in l:
    print(x)

output:
10
20
30
40 50 60 70 80


replace method in python

s="ababa"
print(s)
s1=s.replace('a','b')
print(s1)

output:
ababa
bbbbb

example 2:


s="ababa"
print(s)
print("address of ::",id(s))
s1=s.replace('a','b')
print(s1)
print("address of ::",id(s1))

output:

ababa
address of :: 1358579616712
bbbbb
address of :: 1358567155280


example 3:

string data type is immutable but create the new object



s="ababa"
print(s)
print("address of ::",id(s))
s=s.replace('a','b')
print(s)
print("address of ::",id(s))

output:
ababa
address of :: 1358579616712
bbbbb
address of :: 1358579722816

python class topic video