简体   繁体   English

替换字符串中的奇数和偶数索引字符

[英]Replacing Odd and Even-indexed characters in a string

How can I replace even and odd-indexed letters in my strings?如何替换字符串中的偶数和奇数索引字母? I'd like to replace odd-indexed characters with uppercased letters and even-indexed characters with lowercased ones.我想用大写字母替换奇数索引字符,用小写字母替换偶数索引字符。

x=input("Enter String: ")

How can I modify the inputted string?如何修改输入的字符串?

This sounds a little like a "do my homework for me" post, but I'll help you out, as I need the training myself.这听起来有点像“帮我做作业”的帖子,但我会帮助你,因为我自己需要培训。

You can do this by breaking down the problem.你可以通过分解问题来做到这一点。 (As I am quite new with python syntax, I'm gonna assume that the user has already given an input to string x) (因为我对 python 语法很陌生,我假设用户已经输入了字符串 x)

  1. Make a loop, or otherwise iterate through the characters of your string进行循环,或以其他方式遍历字符串的字符
  2. Make sure you have an index number for each character, which increments for each one确保每个字符都有一个索引号,每个字符都会递增
  3. Check if the number is even, by using modulus of 2 ( %2 ).通过使用模数 2 ( %2 ) 检查数字是否为偶数。 This returns the remainder of a number when divided by 2. In the case of even numbers, that will be 0.这将返回数字除以 2 的余数。在偶数的情况下,这将是 0。
  4. If %2 == 0 set letter to lower case, else set letter to upper case.如果%2 == 0将字母设置为小写,否则将字母设置为大写。
  5. append letter to new String, which you defined before the loop.将字母附加到您在循环之前定义的新字符串。 You cannot directly alter a single character in a String, because they are immutable.您不能直接更改 String 中的单个字符,因为它们是不可变的。 This means that you cannot change the String itself, but you can assign a new String to the variable.这意味着您不能更改字符串本身,但可以为变量分配一个新字符串。
  6. Done.完毕。 Print and see if it worked.打印并查看是否有效。

Code:代码:

x = "seMi Long StRing WiTH COMPLetely RaNDOM CasINg"
result_string = ""
index = 0;
for c in x:
    if(index%2 == 0):
        result_string += c.lower()
    else:
        result_string += c.upper()
    index+=1

print(result_string)
s=input()
l=[]
s=s.lower()
l=[i.upper() if s.index(i)%2==0 else i for i in s ]
print("".join(l))
def myfunc(string):
    result=''
    for x in range(len(string)):
        if x%2==0:
            result=result+string[x].upper() 
        else:
            result=result+string[x].lower()
    return result

The above is a function for the question you asked.以上是您提出的问题的功能。

A non-function for loop might be easier to grasp right now (like you I am very new to Python as well. So for me it was easier to understand the for loop before I got into functions. Look at my next post for the same.一个非函数的 for 循环现在可能更容易掌握(就像你一样,我对 Python 也很陌生。所以对我来说,在我进入函数之前更容易理解 for 循环。看我的下一篇文章也是一样的.

x = 'myname'
for item in range(len(x)):
    if item%2==0:
       print(x[item].upper())
    else:
       print(x[item].lower())

this is the for loop i was referring to.这是我所指的 for 循环。 but the thing with this line of code is that it is specific to the value you have assigned to the variable x where as the function i provided above can take any string value without us having to repeat the code each time.但是这行代码的问题是它特定于您分配给变量 x 的值,因为我上面提供的函数可以采用任何字符串值,而我们不必每次都重复代码。

暂无
暂无

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

相关问题 如何打印字符串的偶数索引和奇数索引字符? - How to print the even-indexed and odd-indexed characters of strings? 对于每个 String S,打印 的偶数索引字符,后跟一个空格,然后是 的奇数索引字符 - For each String S, print 's even-indexed characters, followed by a space, followed by 's odd-indexed characters 给定一个字符串 S,将其偶数索引字符和奇数索引字符作为 2 个空格分隔的字符串打印在一行中 - Given a string, S , print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line 给定一个 string,将其偶数索引和奇数索引字符作为空格分隔的字符串打印在一行上 - Given a string, print its even-indexed and odd-indexed characters as space-separated strings on a single line Pythonic方法将字符串列表转换为字典,奇数索引字符串作为键,偶数索引字符串作为值? - Pythonic way to turn a list of strings into a dictionary with the odd-indexed strings as keys and even-indexed ones as values? 将数组的偶数索引元素乘以 2,将数组的奇数索引元素乘以 3 - Multiply even-indexed elements of array by 2 and odd-indexed elements of array by 3 尝试从字符串中打印偶数索引字符 - Trying to print the even indexed characters from a string 如何使用range(len(list))查找列表中所有偶数索引整数的乘积? - How to use range(len(list)) to find the product of all even-indexed integers in a list? 在 Pandas 中组合奇数和偶数索引行 - Combine odd and even indexed rows in pandas 替换字符串中的特殊字符 - Replacing special characters in a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM