-2

Please write a program which asks the user to type in a string. The program then prints out all the substrings which begin with the first character, from the shortest to the longest. Have a look at the example below.

Please type in a string: test
t
te
tes
test

Obviously my code is not the way it supposed to be:

stg = input("Please type in a string: ")

print(stg[0])
print(stg[0:5])
print(stg[0:10])
print(stg[10:50])
print(stg[:])
1
  • It isnt obvious, does your code not do what you expect it to? Commented Jul 29, 2022 at 14:35

5 Answers 5

2

ok, this is a homework and I don't give you the exact solution... but as some points:

  1. you have a string and want to print first 1 letter, first 2 letters and so on... so your range end must increase one by one...
  2. you don't know about input length so you can't use hard code and use a loop
  3. for loop you need to know about string length and use a builtin method for getting the length...

any question? ask it...

Sign up to request clarification or add additional context in comments.

Comments

0
userString = input("Gimme String: ")
# Looping based on the given String
# Last Value is not included so you need to increment the value by one
for i in range(len(userString)):
    # Last Value is not included so you need to increment the value by one
    print(userString[:i+1])

#Alternative
for i in range(1,len(userString)+1):
    print(userString[:i])

4 Comments

Thank you, comments helped massively. But can i piggyback of this and ask you how to get this result: Please type in a string: test t st est test
@User216345 Just change [:ì] of the alternative implementation in the slice with [:i*-1]
Gimme String: test tes te t
This is what it returned back,while i need it to print out all the substrings which end with the last character, from the shortest to the longest. ` t st est test`
0
stg = input("Please type in a string: ")
print("\n".join([stg[0:i+1] for i in range (len(stg))]))

Output:

t
te
tes
test

Comments

0

Just use simple for loop

stg = 'test'
temp_str = ''
for i in range(len(stg)):
    temp_str = temp_str + stg[i]
    print(temp_str)

4 Comments

Ty sanju can you please explain 'stg[i]' this part specifically the i?
also can ypu loo into this one too i need it to print out all the substrings which end with the last character, from the shortest to the longest. input test ` t st est test `
Let's store my name SANJU in variable string. Now in order to get the first element of my name, S, you need to use indexing. Thus, string[0] would give us S and string[1] would give us A and so on. That is what I am doing above. Getting all the string elements fromstg though indexing. i stores the value of 0,1,2,3, which we are getting by applying range () function on the total length of the string.
@User216345 For second part, stg = 'test' for i in range(1,len(stg)+1): print(stg[ -i:])
0
input_str1 = input("Please type in a string: ")
i = 0
while i < len(input_str1):
    print(input_str1[0:i+1])
    i+=1

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.