Python Forum
How to convert value to defined number?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert value to defined number?
#2
you break a string with split: https://docs.python.org/3.8/library/stdt...#str.split
conversion can be done using a dictionary: https://docs.python.org/3/tutorial/datas...ctionaries
examples:
split:
>>> mystr = "Time is never lost - it's just replaced"
>>> mylist = mystr.split()
>>> mylist
>>> mystr = "Time is never lost - it's just misplaced"
>>> mylist = mystr.split()
>>> mylist
['Time', 'is', 'never', 'lost', '-', "it's", 'just', 'misplaced']
>>> mylist[3]
'lost'
>>>
dictionary:
>>> fruit_color = {
...     'apple': 'red',
...     'orange': 'orange',
...     'banana': 'yellow'
... }
>>> 
>>> print(f"An apple is {fruit_color['apple']}")
An apple is red
>>>
Reply


Messages In This Thread
RE: How to convert value to defined number? - by Larz60+ - Dec-07-2019, 02:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  convert % to a number Nickd12 2 2,095 Sep-18-2020, 12:18 AM
Last Post: deanhystad
  python library not defined in user defined function johnEmScott 2 3,914 May-30-2020, 04:14 AM
Last Post: DT2000
  Find Average of User Input Defined number of Scores DustinKlent 1 4,372 Oct-25-2019, 12:40 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020