Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7499303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:46:56+00:00 2026-05-29T19:46:56+00:00

I am created a pythod method that will take in a string of variable

  • 0

I am created a pythod method that will take in a string of variable length, that will always include a floating point number at the end :

"adsfasdflkdslf:asldfasf-adslfk:1.5698464586546"

OR

"asdif adfi=9393 adfkdsf:1.84938"

I need to parse the string and return the floating point number at the end. There usually a delimiter character before the float, such as : – or a space.

def findFloat(stringArg):
    stringArg.rstrip()
    stringArg.replace("-",":")

    if stringArg.rfind(":"):
        locateFloat = stringArg.rsplit(":")

    #second element should be the desired float
    magicFloat = locateFloat[1]
    return magicFloat

I am recieving a

magicFloat = locateFloat[1]
IndexError: list index out of range

Any guidence on how to locate the float and return it would be awesome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:46:56+00:00Added an answer on May 29, 2026 at 7:46 pm
    1. In Python, strings are immutable. No matter what function you call on a string, the actual text of that string does not change. Thus, methods like rstrip, replace etc. create a new string representing the modified version. (You would know this if you read the documentation.) In your code, you do not assign the results of these calls anywhere in the first two statements, so the results are lost.

    2. Without specifying a number of splits, rsplit does the exact same thing that split does. It checks for splits from the end, sure, but it still splits at every possible point, so the net effect is the same. You need to specify that you want to split at most one time.

    3. However, you shouldn’t do that anyway; a much simpler way to get “everything after the last colon, or everything if there is no colon” is to use rpartition.

    4. You don’t actually have to remove whitespace from the end for float conversion. Although you probably should actually, you know, perform the conversion.

    5. Finally, there is no point in assigning to a variable just to return it; just return the expression directly.

    Putting that together gives us the exceptionally simple:

    def findFloat(stringArg):
        return float(stringArg.replace('-', ':').rpartition(':')[2])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a python module that will be installed on end-user machines.
I have created a Python module that creates and populates several SQLite tables. Now,
I'd like to create a regular expression in Python that will match against a
I have 2 dimensional list created at runtime (the number of entries in either
I'm looking for a python library that will help me to create an authentication
I'm trying to create a form that will handle video file uploads from the
I'm trying to write a script that will automatically update some attachments on a
I need to migrate information that I created in SQLite3 to a MySQL database
I'm creating a socket server that will run forever and push files out to
I created a python project in IDE Anjuta, added some python files and the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.