0

I think this should be fairly simple but can't seem to figure it out.

I just need the python syntax to find if a string contains only numbers.

so it would find: '8888' or '1' or '126928428'

but not: 'Test' or 'Test8'

I searching for it in a for loop that is pulling info off a webpage with BeautifulSoup.

<td>203.195.237.158</td><td>8888</td><td>CN</td><td>China</td><td>Socks5</td>

Basically I am trying to pull the ip and port out of a long string of these.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
RabidGorilla
  • 107
  • 2
  • 10

1 Answers1

4

str.isdigit tests if the string is composed only of characters from '0123456789':

In [35]: 'Test'.isdigit()
Out[35]: False

In [36]: '8888'.isdigit()
Out[36]: True

In [37]: 'Test8'.isdigit()
Out[37]: False
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677