ilay hazenvald ilay hazenvald - 19 days ago 5
Python Question

String array with repeating characters python

I'm trying to break a sentence to characters such as: "the boy was good", and then get the place in the sentence of each letter, but each time i get to letter 'o', the place stays the same for both of the letters. How can I separate those two same letters?

with open("d:\Users\hazembazem\Desktop\python random\crap\encrypt.txt", "rb") as f:
file= f.read()
print file
file= list(file)
for item in file:
a=file.index(item)
print (a)


the file is just a txt file with containing: "the boy was good".

a is meant to be the place of the character, but it's instead shows me this:

0
1
2
3
4
5
6
3
8
9
10
3
12
5
5
15

Answer

string.index(s, sub[, start[, end]])

Like find() but raise ValueError when the substring is not found.


string.find(s, sub[, start[, end]])

Return the lowest index in s where the substring sub is found...


So, yeah, that isn't what you want.

Check this out

with open("filename") as f:
    string = f.read()
    print range(len(string))
    for i,c in enumerate(string):
        print i,c

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
0 t
1 h
2 e
3  
4 b
5 o
6 y
7  
8 w
9 a
10 s
11  
12 g
13 o
14 o
15 d