'How can I call, pull or extract a single letter or character from a variable consisting of multiple alpha characters that has been sorted via split()?

I am unable to split , slice, list or 'x for x in' a variable - specifically a word using alpha characters that has already been pulled from the split and slice function - into separate letters or characters; please see simple program below. The final 'block' or 'object' of text is where I am running into the issue. Maybe it's just the method or approach that I am using from the start of the whole thing. I just need to print the 1st letter of the 2nd word contained in the "enteredPhrase" variable. It must be utilizing variable text entered by the user. Please help. And thanks in advance. PS I removed my "#" comments.

enteredPhrase = input("Enter a phrase or sentence here: ")
print(f"Your phrase or sentence is: {enteredPhrase}")

splitPhrase = enteredPhrase.split()
print(f"The phrase or sentence displayed using the split function in Python: {splitPhrase}")

phraseSize = len(enteredPhrase)
print(f"The size of your phrase or sentence is: {phraseSize}")

secondWord = slice(1, 2)
print(f"The second word in your phrase or sentence is: {splitPhrase[secondWord]}")

finalVariable = splitPhrase[secondWord]
print([x for x in finalVariable])

l = list(finalVariable)
print(l)


Solution 1:[1]

The issue is that using a slice on a list gives another list. Try this for example, which assigns the value at the 0th index of the result of splitPhrase[secondWord] to finalVariable:

#enteredPhrase = input("Enter a phrase or sentence here: ")
enteredPhrase = "This sentence is mine."
print(f"Your phrase or sentence is: {enteredPhrase}")

splitPhrase = enteredPhrase.split()
print(f"The phrase or sentence displayed using the split function in Python: {splitPhrase}")

phraseSize = len(enteredPhrase)
print(f"The size of your phrase or sentence is: {phraseSize}")

secondWord = slice(1, 2)
print(f"The second word in your phrase or sentence is: {splitPhrase[secondWord]}")

finalVariable = splitPhrase[secondWord][0]
print([x for x in finalVariable])

l = list(finalVariable)
print(l)        

Output:

Your phrase or sentence is: This sentence is mine.
The phrase or sentence displayed using the split function in Python: ['This', 'sentence', 'is', 'mine.']
The size of your phrase or sentence is: 22
The second word in your phrase or sentence is: ['sentence']
['s', 'e', 'n', 't', 'e', 'n', 'c', 'e']
['s', 'e', 'n', 't', 'e', 'n', 'c', 'e']

Another (simpler) way to get to the same result is to skip the slice step and replace finalVariable = splitPhrase[secondWord][0] with finalVariable = splitPhrase[1].

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1