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 9004791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:58:42+00:00 2026-06-16T00:58:42+00:00

I want to convert a float between 0.0 and 39.9 into a string. Replace

  • 0

I want to convert a float between 0.0 and 39.9 into a string. Replace the tens digit with an L, T or Y if it’s a 1, 2 or 3 respectively. And append an M if it’s in the ones. For example, 22.3 would return T2.3 and 8.1 would return M8.1 and so forth. Otherwise, return the float.

This code works of course, but I wondering if there was a simpler (if not one-liner) solution. Here’s the code:

def specType(SpT):
  if 0 <= SpT <= 9.9:
    return 'M{}'.format(SpT)
  elif 10.0 <= SpT <= 19.9:
    return 'L{}'.format(SpT - 10)
  elif 20.0 <= SpT <= 29.9:
    return 'T{}'.format(SpT - 20)
  elif 30.0 <= SpT <= 39.9:
    return 'Y{}'.format(SpT - 30) 
  else:
    return SpT

Thanks!

  • 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-06-16T00:58:44+00:00Added an answer on June 16, 2026 at 12:58 am

    How about:

    def specType(SpT):
        return '{}{}'.format('MLTY'[int(SpT//10)], SpT % 10) if 0.0 <= SpT <= 39.9 else SpT
    

    which gives

    >>> specType(0.0)
    'M0.0'
    >>> specType(8.1)
    'M8.1'
    >>> specType(14.5)
    'L4.5'
    >>> specType(22.3)
    'T2.3'
    >>> specType(34.7)
    'Y4.7'
    

    [As noted in the comments, you’ll want to think about what to do with numbers which can sneak through your boundaries — I made one guess; modify as appropriate.]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert a binary number into a float number. Here's an example
I want to convert my float field into a decimal field; I want a
I have some float values I want to convert to a string, I want
I have 2 IEnumerables IEnumerable<float> Distance IEnumerable<XElement> Point which i want to convert into
I have a float number. I want to convert it to a string with
I want to convert a float to a NSDate I converted a NSDate into
I have a string 1/16 I want to convert it to float and multiply
I am trying to convert a float into a binary string. I've noticed a
I want to convert a string to formatted text in C# in WPF application,
I want to convert all the URLs in a javascript string to links, in

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.