Frage

This is a homework assignment. I just need a nudge.

I'm trying to create a loop in which the letters of a string will be removed when it is beyond a certain number.

Example:

Enter Your String: David
Enter Your Amount: 4
Returns: Davi

What I've made so far:

word = raw_input("Enter your string:")
amount = raw_input("Enter your amount:")
word_length = len(word)

if word_length > amount:
  for i in range(word_length):
    if s[i] > amount:

And that's just about as far as I've gotten. I'm not very sure as to which syntax I use to delete the letters that are in positions greater than word_length.

War es hilfreich?

Lösung

A string itself in Python is immutable -- it cannot be changed and letters cannot be deleted. But you can create new string objects based on your string. One way of doing so is slicing.

Read through the linked section of the official Python tutorial until you find what you need. :)

Edit: If you need to use a loop, as suggested in your comment, another technique is also possible:

  1. Create an empty list.

  2. Loop over the indices range(amount) and add the letter corresponding to the current index to your list.

  3. Join the list to a string again using "".join(my_list).

The purpose of the iterim list is that a list can be altered, while a string -- as said before -- is immutable.

Andere Tipps

Do you need to do it in a loop? Otherwise, try string slicing:

word = raw_input("Enter your string: ")
amount = int(raw_input("Enter your amount: "))

s = word[0:amount]

No loops necessary!

word = raw_input("Enter your string:")
amount = int(raw_input("Enter your amount:"))

word = word[:amount]
print word

The first step is to read in your values, and cast the amount to an integer. And since strings are lists of characters at heart, you can get sublists from them. In Python, the [x:y] notation gets a sublist from a list, on the interval [x, y). If you do not provide x ([:y]), the interval becomes [0, y); if you don't provide y ([x:]): [x, len(theStr)); and if you don't provide either ([:]), you get the original string!

Bonus Fun Facts:

The [x:y] operator is an extension of the array subscript operator, [x]. In most languages, calling list[x] will give you the element at x. However, in Python, it can behave more like a traversal. For example, list[-1] will give you the last element in the list.

The [x:y:z] operator also exists, where z is the step interval to use during the traversal. Useful cases for this operator include getting elements at even indices (list[::2]), and reversing the list (list[::-1]).

You may take help of slicing

word = raw_input("Enter your string:")
amount = raw_input("Enter your amount:")
word=word[:int(amount)]   #slicing the rest of unwanted characters
print word

Removing characters from a string in Python is difficult. A better way to approach the given problem would be either with slices, as others have suggested, or if you have to use a loop, by collecting the characters you need in a new string instead of removing them from the old one, ie

s = raw_input()
n = int(raw_input())
new_s = ""
for i in range(n):
    new_s = new_s + s[i]

I guess you have two approaches

  1. Create a new string adding chars one by one from the old until you reach your limit
  2. Overwriting the old string using a slice of itself [:-1] until you reach your limit
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top