1

I am currently trying out Python. I was trying to print out an upside-down pyramid. I'm making the pyramid out of *s and in each line, I have to delete a certain amount of *s from the line. This is my

space = ""
astr3 = "**********"
placeholder2 = 1
while placeholder2 < 10:
    print (space, astr3, "\n")
    space += " "
    astr3 = "*********" #this is supposed to be subtraction
    placeholder2 += 1

Please tell if there is any way to delete strings from strings.

5
  • Please share the expected output. Not clear what you want like this, Commented Dec 30, 2020 at 3:55
  • 1
    I wanted the pyramid to look something like: *********************** ******************** ****************** ************** and so forth. I can figure out the rest of the code; I just need help with deleting the *s each line Commented Dec 30, 2020 at 3:56
  • 2
    Add the expected output in the question itself, not in comments. Commented Dec 30, 2020 at 3:58
  • 2
    to delete a character from a string, set the variable to be the original string without the first character using Python's slicing notation: astr3 = astr3[1:] Commented Dec 30, 2020 at 4:00
  • Did you mean * value will reduce by 1 per line and space value will increase by 1 per line? Commented Dec 30, 2020 at 4:12

3 Answers 3

1

You can use list slicing to slice Strings/Lists. Check it out. You don't need \n in the print statement as it adds a newline. To make a pyramid using your logic, you can delete two * from the string and then print. Remember, it's always better to use intuitive names for the variables. Eg: you can use, idx for index instead of placeholder2.

# Program to print an upside down pyramid
space = ""
astr3 = "**********"
num_stars = len(astr3) # len(astr3) gives number of * in the astr3 string
placeholder2 = 1
while placeholder2 < num_stars // 2:
    display_str = space + astr3 # Adding strings is called concatnation.
    print(display_str) #  This will not add additional space between spaces and astr3
    space += " "
    astr3 = astr3[:-1] # deleting the last element from the list. Called list slicing
    astr3 = astr3[1:] # deleting the first element from the list. Called list slicing
    placeholder2 += 1

The above code outputs the following:

**********
 ********
  ******
   ****
    **

TIP: It's better to share the expected output in the question so that the community can help.

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

Comments

1

Initialize placeholder2 with 0 to get the pyramid from the original string to 1 length string. Since your requirements were to remove the string 1 by per line. To do it set the condition astr3[:-1] and put it to the same variable.

space = ""
astr3 = "**********"
placeholder2 = 0
while placeholder2 < 10:
    print(space, astr3)
    space += " "
    astr3 = astr3[:-1]
    placeholder2 += 1

Output

 **********
  *********
   ********
    *******
     ******
      *****
       ****
        ***
         **
          *

Comments

0

Thank you guys for all the help! I took what you guys said and made the pyramid :D (although my code is very very dumbed down). Here is the code:

space1 = ""
astr4 = "**********"
placeholder3 = 1
while placeholder3 < 7:
    if placeholder3 == 6:
        print (space1, "*")
    print(space1, astr4)
    astr4 = astr4[1:]
    astr4 = astr4[:-1]
    space1 += " "
    placeholder3 += 1

1 Comment

It produces this: ********** ******** ****** **** ** *

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.