How to write a python script, that converts a string format number into 'real' number: integer, float.

0 Robert Trumpet · September 18, 2015
 I need to implement python built-in type convert function: 


The code should convert the following 3 strings into real values:
   (1) "7343345"
   (2) "-87634"
   (3) "98.783"


   What python functions are not allowed:
1.  the type convert function itself: int(), float()


In other words....
Define a function, that takes a string format number as input, and return the number itself. 
e.g. def stringToNumber(str):
         #your code here
           return number

Post a Reply

Replies

- page 2
Oldest  Newest  Rating
0 Bernard Pyc0d333 Parah · September 19, 2015
Agree with Clark 

.isdigit() definitely does the trick

you can also use a third-party module like fastnumbers if you wish

you might have to download the module.


from fastnumbers import fast_real
# you just use pass the number as an argument into the fast real function
fast_real("7343345")

# you can also get the data type thus

type(fast_real("7343345"))


and then you could also always use the yaml method of doing that

import yaml
a = "27733"
a = yaml.load(a)
# you get the real value


def convertToDigit():
...     a = "2343"
...     a = yaml.load(a)
...     print (a)

0 sfolje 0 · September 18, 2015
I dont believe it's possible to convert string to integer without some kind of build-it functions.

You can get for example first or whichever number in string format with:
"7343345"[0]
"7343345"[i]

Or you can get id number of character ( character = string with 1 letter)
ord("a")
ord("7")
  • 1
  • 2

Python

126,337 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator