Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,923 questions

51,856 answers

573 users

How to extract the number from the end of a string in Python

4 Answers

0 votes
def extract_the_number_from_end_of_string(s):
    num = 0
    i = len(s) - 1
     
    # Traverse the string from the end to extract the number
    while i >= 0 and s[i].isdigit():
        num = num + int(s[i]) * (10 ** (len(s) - 1 - i))
        i -= 1
     
    return num
 
 
s = "python java c c++ 194"
 
print(extract_the_number_from_end_of_string(s)) 


 
'''
run:
 
194
 
'''

 



answered Aug 16, 2024 by avibootz
0 votes
import re

def extract_the_number_from_end_of_string(s):
    return re.search(r"(\d+)$", s).group()
 
 
s = "python java c c++ 194"
 
print(extract_the_number_from_end_of_string(s)) 


 
'''
run:
 
194
 
'''

 



answered Aug 16, 2024 by avibootz
0 votes
def extract_the_number_from_end_of_string(s):
    return ''.join([char for char in s[::-1] if char.isdigit()])[::-1]
 
 
s = "python java c c++ 194"
 
print(extract_the_number_from_end_of_string(s)) 


 
'''
run:
 
194
 
'''

 



answered Aug 16, 2024 by avibootz
0 votes
def extract_the_number_from_end_of_string(s):
    return ''.join(filter(str.isdigit, s[::-1]))[::-1]
 
 
s = "python java c c++ 194"
 
print(extract_the_number_from_end_of_string(s)) 


 
'''
run:
 
194
 
'''

 



answered Aug 16, 2024 by avibootz

Related questions

2 answers 110 views
2 answers 104 views
1 answer 94 views
1 answer 103 views
2 answers 105 views
2 answers 110 views
2 answers 98 views
...