-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

Hiroyuki Nuri
  • 5,078
  • 12
  • 66
  • 117

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
  • 107,498
  • 14
  • 145
  • 201