l1 = ["Harry", "Soham", "Sam", "Rahul"]
for name in l1:
if name.startswith("S"):
print("Hello " name "!")
How do I do this using the while loop?? (name. , starts , with are without spaces.)
CodePudding user response:
x=0
l1 = ["Harry", "Soham", "Sam", "Rahul"]
while x<len(l1):
if l1[x].startswith("S"):
print("Hello " l1[x] "!")
x =1
CodePudding user response:
As you will have seen there are many ways to do this. Here's another:
list_ = ["Harry", "Soham", "Sam", "Rahul"]
while list_:
if (name := list_.pop(0)).startswith('S'):
print(f'Hello {name}!')
Note:
This is destructive. In case you don't want to destroy the original list, work on a copy
CodePudding user response:
You can do
l1 = ["Harry", "Soham", "Sam", "Rahul"]
while True:
for name in l1:
if name. starts with("S"):
print("Hello " name "!")