Shift Numbers in Python | Assignment Expert

Hello friends, in this article you will learn how to shift numbers in Python in a string from any place of string to the start of the string. For example, making bnj67jbn to 67bnjjbn, simply we need to remove integers from inside of a string and place them to start of the string. To check whether a character is a digit or not, we can use isdigit() method of Python.

Code to shift numbers in Python:

mystr='bnj67jbn'
mylist1=[]
mylist2=[]
for i in range(len(mystr)):
    if mystr[i].isdigit():
        mylist1.append(mystr[i])
    else:
        mylist2.append(mystr[i])
print(''.join(mylist1)+''.join(mylist2))

Output:

Shift Numbers in Python | Assignment Expert

Explanation:

So, we created a string, declared two empty lists, then inside for loop, we checked each character of string whether it is digit or not. If its a digit, we will append it to list 1 else list 2. At last, we are printing both lists by converting them to strings(using join() method) and joining them with + operator.


Also Read:

Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you