Write a Python program to find the character position of Kth word from a list of strings
The program is a python script that returns the character at the Kth position of the first word in a list of strings.
- A list of strings "val" is defined with 4 strings.
- The original list "val" is printed using the "print()" function.
- A nested list comprehension is used to create a list "res" of all the first characters of each word in the list "val".
- The "enumerate()" function is used to get the index and the value of each string in the list "val".
- Another "enumerate()" function is used to get the index and the value of each character in each string in the list "val".
- The nested list comprehension generates a list of tuples, where each tuple consists of the index of the word and the index of the character.
- The index of the character at the Kth position in the first word is accessed using the "res[K]" notation.
- The final result is printed using the "print()" function.
Source Code
val = ["Tutor","joes","Computer","Education"]
print("The original list is : " ,val)
K = 20
res = [i[0] for sub in enumerate(val) for i in enumerate(sub[1])]
res = res[K]
print("Index of character at Kth position word : " + str(res))
Output
The original list is : ['Tutor', 'joes', 'Computer', 'Education']
Index of character at Kth position word : 3