简体   繁体   English

如何获取字符串中的最后一个字符?

[英]How to get last character in string?

I want to find which word's last character is 'e' and I would like to replace 'e' with 'ing'.我想找出哪个单词的最后一个字符是'e',我想用'ing'替换'e'。 After this process want to append these in the array such as new words这个过程之后想append这些在数组里等新词

words= ['example', 'serve', 'recognize', 'ale']


for x in words:
    size = len(x)
    if "e" == x[size - 1]:
       words.append(x.replace(x[-1], 'ing'))

print(words)

output output

['example', 'serve', 'recognize', 'ale', 'ingxampling', 'singrving', 'ringcognizing', 'aling']

I want to get the output like this我想像这样得到 output

['example', 'serve', 'recognize', 'ale', 'exampling', 'serving', 'recognizing', 'aling']

Try this:尝试这个:

words = ['example', 'serve', 'recognize', 'ale']

for x in words:
    if x[-1] == 'e':
       words.append(x[:-1] + 'ing')

print(words)

Or if you want a 1 liner:或者如果你想要一个 1 班轮:

words = [*words, *[x[:-1] + 'ing' for x in words if x[-1] == 'e']]

Very similar to saradartur's solution but with filtering (I also added the use of str.endswith ):与 saradartur 的解决方案非常相似,但带有过滤功能(我还添加了str.endswith的使用):

words = ['example', 'serve', 'recognize', 'ale']
words.extend(word[:-1] + 'ing' for word in words if word.endswith('e'))
print(words)

Output Output

['example', 'serve', 'recognize', 'ale', 'exampling', 'serving', 'recognizing', 'aling']

The answer to "How to get last character in string on Python?" “如何在 Python 上获取字符串中的最后一个字符?”的答案is quite simple:很简单:

my_string = "hello"

last_char = last_char = my_string[-1:]
print(last_char)

>>> o

This can then be applied to solving what your code is trying to do:然后可以将其应用于解决您的代码尝试执行的操作:

words= ['example', 'serve', 'recognize', 'ale']

for x in words:
    last_char = x[-1:]
    if last_char == "e":
        words.append(x[:-1]+"ing")

print(words)

>>> ['example', 'serve', 'recognize', 'ale', 'exampling', 'serving', 'recognizing', 'aling']

Looks like you don't really want to get the last character but check the last character.看起来你真的不想得到最后一个字符,而是检查最后一个字符。 Anyway, a version that can handle arbitrarily long suffixes:无论如何,一个可以处理任意长后缀的版本:

>>> suffix, replacement = 'e', 'ing'
>>> for word in words:
        if word.endswith(suffix):
            print(word.removesuffix(suffix) + replacement)

exampling
serving
recognizing
aling

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在某个字符之前获取字符串的最后一部分? - how to get the last part of a string before a certain character? 如何使用正则表达式获取不是特定字符的字符串中的最后一个字母? - How to get the last letter in a string that is not a specific character using regex? 如何查找字符是否是字符串中的最后一个字符? - How to find if character is the last character in a string? 如何按字符串的最后一个字符对列表进行排序 - How to sort a list by last character of string 如何将字符串的最后一个字符分配给变量? - How to assign the last character of a string to a variable? 如何根据第一个和最后一个字符替换字符串 - How to replace a string based on first and last character 如何获取和更新python数据帧中某些字符(例如“ \\”)后的最后一个字符串 - How to get and update last string after some character like '\ ' in python dataframe 除了 Python 中的最后一个字符之外,如何获取字符串中的特定字符? - How can I get the specific character in string except the last one in Python? 如何从最后一个“。”之后的字符串中删除最后一个字符 - How do i remove the last character from a string after the last “.” 如何从 Python 获取文件中的最后一个字符? - How to get the last character in a file from Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM