-1

I need to find the character that is in certain position, which will be a value entered by the user, but I can't find a way to get the character in said position.

For example: If the user typed in the phrase "I need a plane" and he selected position number 3, I would need to show the user the letter "e". However, I cannot find a way to get the character in x position.

However, I cannot use an index because the position I need is not defined, it is a value that the user will give, and Python only allows integers to be entered in that section.

john-hen
  • 4,410
  • 2
  • 23
  • 40

1 Answers1

0

You can index a string just like an array in python:

s = "I need a plane"
print(s[3])

Output:

e

To get the string and position as user input you can use the input() function, and parse the second string to an integer:

s = input("Please enter a string: ")
i = input("Please enter an index: ")
i = int(i)
print(s[i])

With the input "I need a plane" and "3" the output will be:

e
Erik McKelvey
  • 1,650
  • 1
  • 12
  • 23