1

I am trying to make one program using python where User has to put value which is equal to any name or any word and what it does is that it cut first letter of the word and paste it at the end of the word and adds 2 more alphabet equal to 'py'.


Ex- Enter the value: Shashank 'Output which i am getting is shashankspy' but the value which I want is 'hashankspy'

The code which i made is this:

pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    print original

    word = original.lower()
    first = word[0]
    new_word = word + first + pyg
    new_word[1:]


else:
    print 'empty' 

I am not able to get the real value. Please help!

4 Answers 4

2

There is one simple error which you are making and it can be done as:

new_word = new_word[1:len(new_word)]

    pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    print original

    word = original.lower()
    first = word[0]
    new_word = word + first + pyg
    new_word = new_word[1:len(new_word)]


else:
    print 'empty'
Sign up to request clarification or add additional context in comments.

Comments

2

You need to stake a slice from the word [1:]:

new_word = word[1:] + first + pyg

Also, instead of checking the length for being more than zero, just check for emptiness:

if original and original.isalpha():

Demo:

>>> def make_new_word(s):
...     pyg = 'py'
...     if s and s.isalpha():
...         word = s.lower()
...         first = word[0]
...         new_word = word[1:] + first + pyg
...         return new_word
...     else:
...         return 'empty' 
... 
>>> original = raw_input('Enter a word: ')
Enter a word: Shashank
>>> make_new_word(original)
'hashankspy'

5 Comments

Well thanks your answer is correct but what's the problem i am facing when i type this code new_word = word + first + pyg new_word[1:]
@ShashankSharma you just don't print it.
Ya I know i didn't mention here but i have putted that syntax ... but after typing new_word[1:] and print new_word why i cant get the real value as true.
@ShashankSharma you needed to just print new_word[1:].
new_word[1:], by itself, does not print anything and does not change new_word. It simply computes that slice, and then throws it away since you haven't specified anything to do with it.
0

The minute you put in a space, original.isalpha() becomes false because a space is not A-Z.

Comments

0

You can also do like adding

new_word = new_word[1:len(new_word)]


pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    print original

    word = original.lower()
    first = word[0]
    new_word = word + first + pyg
    new_word = new_word[1:len(new_word)]


else:
    print 'empty'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.