How to convert strings into integers in Python?

后端 未结 15 1904
自闭症患者
自闭症患者 2020-11-22 01:33

I have a tuple of tuples from a MySQL query like this:

T1 = ((\'13\', \'17\', \'18\', \'21\', \'32\'),
      (\'07\', \'11\', \'13\', \'14\', \'28\'),
               


        
15条回答
  •  长发绾君心
    2020-11-22 01:48

    Python has built in function int(string) and optional parameter base.

    if your string contains an Integer value, it will convert that to the corresponding Integer value. However if you have decimnal number as string you'll need float() to convert it.

    Usage:

    a = '22'
    b = int(a)
    

    and

    if a = '22.22'
    b = int(a) '''will give error, invalid literal for int().'''
    b = float(a) '''will convert the string.'''
    

提交回复
热议问题