Requirement:
Sample Input: s4b6c5a2d9
Output: ssssbbbbbbcccccaaddddddddd
string_data=input("Enter string: ") result='' for character in string_data: if character.isalpha(): alphabet=character else: digit=int(character) result=result+alphabet*digit print("You entered: ",string_data) print("Final result: \n", result )
Requirement:
Sample Input:ssssbbbbbbcccccaaddddddddd
Output: 4s6b5c2a9d
string_data=input("Enter string: ")
result=''
previous=string_data[0]
character_count=1
i=1
while i<len(string_data):
if string_data[i]==previous:
character_count=character_count+1
else:
result=result+str(character_count)+previous
previous=string_data[i]
character_count=1
if i==len(string_data)-1:
result=result+str(character_count)+previous
i=i+1
print("You entered: ",string_data)
print("Final result: \n", result )
No comments:
Post a Comment