'Making if statement if the character in the index is equal to [closed]
How to make an if statement only giving the index of character in the string and if its equall to example
# i know this is wrong so plss help me
k = "klk"
if k[2] == "l":
print("done")
If i print its only program finish
Solution 1:[1]
Python indexes start at 0.
k[0] is k
k[1] is l
k[2] is k
You would need to do
k = "klk"
if k[1] == "l":
print("done")
You can also go in a reverse form with negative numbers:
test = "hello"
if test[-2] == "l":
print("done")
test[-1] is o
test[-2] is l
test[-3] is l
test[-4] is e
Solution 2:[2]
k = input("enter example: ")
if k[1] == "l" and len(k) >= 2:
print("k contains l")
I would highly recommend Tech With Tim's video "Python as fast as possible": https://www.youtube.com/watch?v=VchuKL44s6E
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 | |
| Solution 2 |
