DEV Community

Kuhanraja A R
Kuhanraja A R

Posted on

18 april at payilagam (For loop using string)

For Statement:

The for statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.

# Vowels in name
name = "kuhan"
print("aeiou" in name)

sen = "today is a friday"
print("friday" in sen)
print("abcd" not in sen)
print("fri" in sen)
print("abcd" not in sen)
Enter fullscreen mode Exit fullscreen mode
# indexing
sentence = input("enter name : ")
print("Your First letter is : ",sentence[0])
print("Your Last letter is : ",sentence[-1])

sen = "salman"
for every_letter in sen:
    print(every_letter[0:1]) 
Enter fullscreen mode Exit fullscreen mode
name = "mohd salman"
for letter in name:
    print(letter)
Enter fullscreen mode Exit fullscreen mode
# count of y:

count = 0
sen="today is friday"
for letter in sen:
    if letter == 'y':
        count+=1
else:
    print(count)
Enter fullscreen mode Exit fullscreen mode
#Count of vowels

count_of_vowels=0
vowels = "aeiou"
name = "kuhanraja"
for every_letter in name:
    if every_letter in vowels:
        count_of_vowels+=1
else:
    print(count_of_vowels)

count_of_vowels = 0
vowels = "aeiou"
name = "kuhanraja"
for every_letter in name:
    if every_letter == 'n':
        break
    print(every_letter,end=' ')
else:
    print("Hi")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)