Python 从字符串中删除字母
这是一项家庭作业。我只需要轻轻一推。
我正在尝试创建一个循环,其中当字符串超过某个数字时,该字符串的字母将被删除。
示例:
Enter Your String: David
Enter Your Amount: 4
Returns: Davi
到目前为止我所做的:
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:
这就是我所取得的成果。我不太确定使用哪种语法来删除位置大于 word_length
的字母。
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 Python 中从字符串中删除字符很困难。解决给定问题的更好方法是使用切片,正如其他人所建议的那样,或者如果您必须使用循环,则通过在新字符串中收集所需的字符而不是从旧字符串中删除它们,即
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
我猜你有两种方法
I guess you have two approaches
Python 中的字符串本身是不可变的——它不能更改,也不能删除字母。但是您可以根据您的字符串创建新的字符串对象。一种方法是切片。
通读官方 Python 教程的链接部分,直到找到您需要的内容。 :)
编辑:如果您需要使用循环,如评论中建议的那样,也可以使用另一种技术:
创建一个空列表。
循环索引
range(amount)
并将当前索引对应的字母添加到列表中。使用
"".join(my_list)再次将列表连接到字符串
。iterim 列表的目的是列表可以更改,而字符串(如前所述)是不可变的。
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:
Create an empty list.
Loop over the indices
range(amount)
and add the letter corresponding to the current index to your list.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.
您需要循环执行吗?否则,尝试字符串切片:
Do you need to do it in a loop? Otherwise, try string slicing:
无需循环!
第一步是读入您的值,并将金额转换为整数。
由于字符串本质上是字符列表,因此您可以从中获取子列表。
在 Python 中,
[x:y]
表示法从列表中获取区间 [x, y) 上的子列表。如果不提供x(
[:y]
),则区间变为[0, y);如果您不提供 y ([x:]
): [x, len(theStr));如果您不提供任何一个 ([:]
),您将获得原始字符串!额外有趣的事实:
[x:y]
运算符是数组下标运算符[x]
的扩展。在大多数语言中,调用list[x]
将为您提供 x 处的元素。然而,在 Python 中,它的行为更像是遍历。例如,list[-1]
将为您提供列表中的最后一个元素。[x:y:z]
运算符也存在,其中 z 是遍历期间使用的步长间隔。此运算符的有用案例包括获取偶数索引处的元素 (list[::2]
) 和反转列表 (list[::-1]
)。No loops necessary!
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, callinglist[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