DEV Community

Guru prasanna
Guru prasanna

Posted on

Python Day 6- String Functions,Looping-For,ifelse conditions and Task

Find():
Searches the string for a specified value and returns the position of where it was found.
Ex:

txt = "Hello, welcome to my world."

x = txt.find("welcome")

print(x)

Enter fullscreen mode Exit fullscreen mode

Output:

7

Enter fullscreen mode Exit fullscreen mode

So welcome is in 7th position as per indexing.If any other word is given which is not defined then the result will be -1.

Note:In the above example if index function is used instead of find,then it will display "valueerror: substring not found".If defined then output will be same like find function.

Looping:
For loop:
Ex:1

txt = '1234'

for num in txt:
    print(num,end=' ')

Enter fullscreen mode Exit fullscreen mode

Output:

1 2 3 4
Enter fullscreen mode Exit fullscreen mode

Ex:2

name = input("Enter Name: ")
print(name)
for alphabet in name:
    print(alphabet, end='*') 
Enter fullscreen mode Exit fullscreen mode

Output:

Enter Name: guru
guru
g*u*r*u*
Enter fullscreen mode Exit fullscreen mode

Ifelse:
It runs the program with decision based on whether the statement is true or false.

Image description
Ex:

txt = '12a4'

for num in txt:
    if num>='0' and num<='9':
        print(num,end=' ')
    else:
        print('Not Decimal',end=' ')

Enter fullscreen mode Exit fullscreen mode

Output:

1 2 Not Decimal 4 
Enter fullscreen mode Exit fullscreen mode

In above example 1,2,4 are decimal but a is not a decimal so in output it shows not decimal as per else condition.

Task:

lakshmi Pritha
guru Prasanna
guhanraja
varatharajan

Find:
1: Names starting with letter 'g'
2: Names endings with 'a'
3: Names having space in between
4: Names having more than 9 letters

name=input("Enter names: ")
names=(name).split(",")

for letter in names:
    if letter.startswith('g'):
        print("Names starts with g are: ",letter)
for ending_letter in names:
    if ending_letter.endswith('a'):
        print("Names end with a are: ",ending_letter)
for space in names:
    for word in space:
        if word==' ':
            print("Names with space: ",space)
        else:
            continue
for character in names:
    if len(character)>9:
        print("Names with more than 9 letters: ",character)
Enter fullscreen mode Exit fullscreen mode

output:

Enter names: guru prasanna,guhanraja,lakshmi pritha,varatharajan
Names starts with g are:  guru prasanna
Names starts with g are:  guhanraja
Names end with a are:  guru prasanna
Names end with a are:  guhanraja
Names end with a are:  lakshmi pritha
Names with space:  guru prasanna
Names with space:  lakshmi pritha
Names with more than 9 letters:  guru prasanna
Names with more than 9 letters:  lakshmi pritha
Names with more than 9 letters:  varatharajan



Enter fullscreen mode Exit fullscreen mode

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay