-5

How can I get the first characters in a for using Python ?

Let's say we have number = ['078 823 42', '021 932 02']

For num in number: - Get the number with 07 at begining and print it.

if I use this:

if num[0:2] == '07':
    print(datas.append(num.string))

I get this error TypeError: unhashable type: 'slice'

I am using python 3.3 and beautifulsoup

Horai Nuri
  • 5,358
  • 16
  • 75
  • 127

1 Answers1

2

You can simply use slicing to get the first two characters of each string.

for num in number:
    print(num[0:2])
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218