Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want an output like if I entered a name "hello"

then the output should be as follows:-
olleh
olle
oll
ol
l

What I have tried:

m= input("Enter name: ")
n=len(m)
for i in range(n+1):
print([n+1:i-1:1])

I know that as a learner my code is wrong
Posted
Updated 1-Nov-22 8:04am

Look at what you are supposed to print:
First line 5 characters
Second line 4 characters
...
Last line 1 character

So you just need to print the first N characters of the word each time, counting down from the word length to 1.

See 3. An Informal Introduction to Python — Python 3.11.0 documentation[^] for how to extract parts of a string.
 
Share this answer
 
Well, first, you're going to have to reverse your string, which you are not doing.

Next, you have to create a range that goes from n to 0. What would be the stop value for that?

After that, you need to print a substring of you string. THe code doesn't work because you didn't tell the print statement what string to print. What variable is holding your string?

And lastly, you need to rethink how you'e specifying the substring to print, specifically, what values/variables are going in the [::] substring spec. Hint: You only need to specify one value. I leave it to you to figure out which position between the brackets and colons.
 
Share this answer
 
Comments
Dave Kreskowiak 1-Nov-22 14:38pm    
Well, you got the range wrong, and you're really not thinking about the substring arguments. WRite your "Hello" on paper and go through the code, step by step, keeping track of variables on paper. This should make it a lot easier to figure out.

Varshith 10 C 1-Nov-22 14:47pm    
sir
name = input("Enter name:")
reversed_name = name[::-1]
print(reversed_name)
for i in range(1,len(name)):
print(reversed_name[:-i])


and sir thanks a lot it helped me
now its working
I mistakenly deleted the above reply sorry for that
Varshith 10 C 1-Nov-22 14:51pm    
and sir I need to be very serious about learning python how can I start I feel lazy I want to get rid of it. Can u help me I want to learn how to think my mind is capable of thinking but I am not able to think so much I want some suggestions on learning python how to start plz if u have time u can reply :>
Dave Kreskowiak 1-Nov-22 15:47pm    
Your best source is going to be books on Python. DO NOT USE YOUTUBE "TUTORIALS"!
Richard MacCutchan 1-Nov-22 15:57pm    
Follow the link I gave you in my answer. It takes you to a complete Python tutorial.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900