Sunday, November 10, 2019

merging strings in python

s1=input("enter the first string::")
s2=input("enter the second string::")
output=''
i=j=0
while i<len(s1) or j<len(s2):
    output=output+s1[i]+s2[j]
    i=i+1
    j=j+1
print(output)

output:
enter the first string::anil

enter the second string::durg
adnuirlg


example 2:
s1=input("enter the first string::")
s2=input("enter the second string::")
output=''
i=j=0
while i<len(s1) or j<len(s2):
    if i<len(s1):
        output=output+s1[i]
        i=i+1
    if i<len(s2):
        output=output+s2[j]
        j=j+1
print(output)

output:
enter the first string::anil

enter the second string::durgam
adnuirlgam

printing unicode character in place of numbers in python

s= input("Enter some string:")
output=''
for x in s:
    if x.isalpha():
        output=output+x
        previous=x
    else:
        newch=chr(ord(previous)+int(x))
        output=output+newch
print(output)

output:
Enter some string:a4b2d9
aebddm

using sorted keyword and extracting alphabets and numbers using python

s= input("Enter some string:")
s1=s2=output=''
for x in s:
    if x.isalpha():
        s1=s1+x
    else:
        s2=s2+x
print(s1)
print(s2)


output:
Enter some string:a1b2c3
abc
123

example 2:
s= input("Enter some string:")
s1=s2=output=''
for x in s:
    if x.isalpha():
        s1=s1+x
    else:
        s2=s2+x
for x in sorted(s1):
    output=output+x
for x in sorted(s2):
    output=output+x
print(output)

output:
Enter some string:a1b2c3d4
abcd1234


example 3:
s= input("Enter some string:")
output=''
for x in s:
    if x.isalpha():
        output=output+x
        previous=x
    else:
        output=output+previous*(int(x)-1)
print(output)


output:
Enter some string:a4b2c5d2
aaaabbcccccdd

functions in python

def test():     print("good morning") test() output: good morning