Python program for adding given string with a fixed message

Here, we will figure out how to include a given string with a fixed message/string utilizing python program?

Given a string and we need to include a welcome message like Hello with the string and return it utilizing python program.

Here in this instructional exercise, we would figure out how to utilize strings in Python? We would code here to include various strings together. At first, we should recognize what a string is in this way, a String in python is encompassed by single statements or twofold statements for example ‘ ‘ or ” “.

Example:

    print('Hello World') 
    #or
    print("Hello World") 

This would show Hello World as an output.

Presently beginning with our inquiry,

Question:

We are given with string of certain people groups name and all we got the opportunity to do is to include “Hello” before their names so as to welcome them. In the event that the string as of now starts with “Hello“, at that point return the string unaltered.

Example:

    greeting('Kishan') = 'Hello Kishan'
    greeting('Durgesh') = 'Hello Durgesh'
    greeting('Hello Aakash') = 'Hello Aakash'

Arrangement:

Presently this issue is somewhat unpredictable as we need to check whether “Hello” is connected at first or not!

We can take care of this issue by simply including an if articulation/statement.

Code:

def greeting(str):
    if len(str) >= 5 and str[:5] == 'Hello':
        return str
    return 'Hello ' + str

print (greeting('Kishan'))
print (greeting('Durgesh'))
print (greeting('Hello Aakash!'))

Output:

Hello Kishan
Hello Durgesh
Hello Aakash!

In the last line of our code while composing string Hello we have given space with the goal that Hello and the name don’t combine and look terrible.

Leave a Comment

error: Alert: Content is protected!!