Sunday, November 10, 2019

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

No comments:

Post a Comment

python class topic video