Reputation: 1
I'm trying to solve a problem where I wish to select multiple words beginning with 's', but have both the lowercase and uppercase words be shown in the output.
To try and achieve my desired result, I used an if-else statement in a for-loop but it only selected the first value I chose. For example, if I wrote 'S' first, it selected the uppercase and ignored the lowercase.
st = 'Sally sells shakes from Shake Shack every Saturday.'
for i in st.split():
if i[0] == 's' and 'S':
print(i)
I expected the output to be: sells shakes Sally Shake Shack Saturday
But the actual output was: sells shakes
Upvotes: 0
Views: 121
Reputation: 7206
use logical OR: Return True if one of the statements are True : Your word will start with 'S' or 's'
NOTE: i[0] == 's' and 'S', is not correct expression in this case
STATEMENT1: i[0] == 's'
STATEMENT2: i[0] == 'S'
if STATEMENT1 or STATEMENT2
st = 'Sally sells shakes from Shake Shack every Saturday.'
for i in st.split():
if i[0] == 's' or i[0] == 'S':
print(i)
Upvotes: 1
Reputation: 5958
You can use lower()
to make comparison lowercase:
st = 'Sally sells shakes from Shake Shack every Saturday.'
for i in st.split():
if i.lower().startswith('s'):
print(i)
result:
Sally
sells
shakes
Shake
Shack
Saturday.
Upvotes: 2