How To Convert String To Int In Python

preview_player
Показать описание
In this python tutorial, we discuss converting string to ints! In particular, we talk about how to convert string to int in python and some of the gotchas you need to watch out for! Let's hop right in and start coding!

Video Timeline:
0:00:00 - Video Overview
0:00:39 - String To Int
0:02:15 - Like And Subscribe
0:02:28 - String To Int Gotchas
0:08:59 - Thank You

======== Python Tutorials ========

======== Python Questions ========

#CaseDigital #pythontutorial #pythonstrint2int
Рекомендации по теме
Комментарии
Автор

Thanks man. Just spent an hour and a half banging my head against the wall trying to convert a string element ("3.0") of a mixed list to an int, but was inexplicably getting a dreaded error, until I discovered your tutorial.

string = "Doc. Version 3.0"

mylist = string.split()

alphanum = (mylist[2])

alphanum = int(alphanum) # Thought this would do it, but nah.

print(alphanum)

Sad output:
Traceback (most recent call last):
File "D:\Programming\Python\Python_scripts\thonny_practice_scripts\string_to_int_check.py", line 6, in <module>
alphanum = int(alphanum)
ValueError: invalid literal for int() with base 10: '3.0'

Turns out all I had to do was use a float as you suggested:

alphanum = float(alphanum)

Output: 3.0

Voila!

type(alphanum)
<class 'float'>
("3.0")

Never even considered that the document version number was a float! I just thought of it as version 3.

JohnnyJazzFreak