Home > Enterprise >  Can´t with separating string using indexes
Can´t with separating string using indexes

Time:09-30

I want to make a code that receives a string and if said string has more than 30 characters it get every letter after 30 and creates a second line using turtle.

I developed this code, however it only gets the index of the first occurance of a letter (i tested with the question variable being '12345789123456789123456789123456789'

prilin=''
seglin=''
question=q[0]
for i in question:
  print(question.index(i))
  if int(question.index(i))>30:
   seglin=seglin i
  elif int(question.index(i))<30:
    prilin=prilin i
t.write(prilin,False,'left',('Arial', 12, 'normal')) 
t.rt(180)
t.fd(25)
t.write(seglin,False,'left',('Arial', 12, 'normal')) 

Any help would be greatly appreciated.

CodePudding user response:

You can slice the string to skip over the first 30 letters.

q = "12345789123456789123456789123456789"
print(len(q)) # 35
print(q[30:]) # 56789

CodePudding user response:

Using the index function will always return the index of the first occurrence of the value that is passed to it. Rather than iterating by character and then trying to find the index of each character, you can iterate through the indices, and then find the character at that index.

for i in range(len(question)):
  print(i)
  if i > 30:
    seglin = seglin   question[i]
  elif i < 30:
    prilin = prilin   question[i]
  • Related