How can I upper case the letter with an even index in a string

how can i convert each alternate character of a string to uppercase in python
how to print alternate characters of a string in python
convert the alternate characters of the string to uppercase in python
write a generator that alternates between returning even and odd
python capitalize every other letter
the ----------function returns the string in a lowercase letter in php
how to change the case of a character in java
the ----------function returns the string in a lowercase letter in mysql

Here is my thought: I want to create new empty string (newst), and add letters in the tested string (st) one by one by testing and upper or lower case the letter depends on the even or odd index.

Below is my code:

def myfunc(st):
    newst = ''
    for i in st:
        if st.index(i) %2 == 0:
            newst = newst + i.upper()
        else:
            newst = newst + i.lower()
    return newst

When I test the code:

myfunc('bbAA')

I got: 'BBAA'

Can anyone help me to explain why I got the incorrect result? Thank you

Don't use .index in a loop to find the index of the current value; it finds the index of the first occurrence of that value, not necessarily the occurrence you're currently looking at. Iterate using enumerate to get both the index and value together:

def myfunc(st):
    newst = ''
    for index, char in enumerate(st):
        if index % 2 == 0:
            newst = newst + char.upper()
        else:
            newst = newst + char.lower()
    return newst

Similar solution using join and a comprehension:

def myfunc(st):
    return ''.join(
        char.upper() if index % 2 == 0 else char.lower()
        for index, char in enumerate(st)
    )

How to uppercase even letter and lowercase odd letter in a string , odd character of string to upper case and even positioned characters the string and convert odd elements to uppercase and even to lower� Return value from String upper() upper() method returns the uppercased string from the given string. It converts all lowercase characters to uppercase. If no lowercase characters exist, it returns the original string.

st.index(i) is returning the first index that has that character, so bbAA finds the index of the first b for both the first and second b. It's also highly inefficient, since it needs to perform a linear search for each character.

It's an easy fix though; the enumerate function will give you the index along with the value for free:

def myfunc(st):
    newst = ''
    for i, char in enumerate(st):
        if i % 2 == 0:
            newst += char.upper()
        else:
            newst += char.lower()
    return newst

Note that this does perform a decent amount of moderately expensive math for no reason (you're just alternating after all). One way to avoid it would be to have itertools.cycle switch back and forth between upper and lower for you:

from itertools import cycle

def myfunc(st):
    newst = ''
    for char, op in zip(st, cycle((str.upper, str.lower))):
        newst += op(char)
    return newst

Note that while CPython will handle repeated str appends relatively efficiently, it's only an implementation detail that you can't rely on. A more portable way to do this efficiently might be to use a list comprehension or generator expression and join it:

def myfunc(st):
    return ''.join([op(char) for char, op in zip(st, cycle((str.upper, str.lower)))])

Python, Every piece of data and even functions and types are objects. Test yourself: How would you write the expression to produce a lowercase version of the string s ? Python indexes the characters in a string, starting from 0, so for instance, the� The problem of case changes a string is quite common and has been discussed many times. Sometimes, we might have a problem like this in which we need to convert the odd character of string to upper case and even positioned characters to lowercase. Let’s discuss certain ways in which this can be performed. Method #1 : Using upper() + lower

string.index returns the first position where the letter you are searching is found, you should use enumerate to get the letter and its index

def myfunc(st):
    newst = ''
    for index,alpha in enumerate(st):
        if index %2 == 0:
            newst = newst + alpha.upper()
        else:
            newst = newst + alpha.lower()
    return newst


print (    myfunc("aaaBBB"))

And output is:

$ python3 hack07.py

AaAbBb

2.1. Strings, Part III — Hands-on Python Tutorial for Python 3, The string upper() method converts all lowercase characters in a string into uppercase characters and returns it. The ToUpper method is often used to convert a string to uppercase so that it can be used in a case-insensitive comparison. A better method to perform case-insensitive comparison is to call a string comparison method that has a StringComparison parameter whose value you set to StringComparison.CurrentCultureIgnoreCase for a culture-sensitive, case-insensitive comparison.

Why not use str.join and a comprehension:

def myfunc(st):
    return ''.join(s.lower() if i % 2 else s.upper() for i , s in enumerate(st))

>>> myfunc('aaBB')
AbAb

Python String upper(), But in Python, the index is an offset from the beginning of the string, and the offset Python does not handle uppercase and lowercase letters the same way that� Python string method upper() returns a copy of the string in which all case-based characters have been uppercased. Syntax. Following is the syntax for upper() method − str.upper() Parameters. NA. Return Value. This method returns a copy of the string in which all case-based characters have been uppercased. Example

Chapter 6: Strings - Textbooks, Changing a string to uppercase, lowercase, or capitalize. Slice- it gives the letter from the given index, a[1] will give "u" from the word Guru as such In Python, you can even change the string to upper case or lower case. 1. Select the strings you want to change their cases, and click Kutools > Text > Change Case. See screenshot: 2. Then in the popping Change Case dialog, check the option you want in the Change type section, and you can preview the changing result in the Preview pane. See screenshot: 3. Then click Ok or Apply to change case.

Python Strings: Replace, Join, Split, Reverse, Uppercase & Lowercase, def myfunc(string): new_string = '' for x in range(len(string)): if (x+1)%2==0: new_string += to see if the index is even with mod 2 result+=l.upper() #it's even . upper the char and append it we append the letter at that index but in upper case. Definition and Usage. The index() method finds the first occurrence of the specified value.. The index() method raises an exception if the value is not found.. The index() method is almost the same as the find() method, the only difference is that the find() method returns -1 if the value is not found.

solution_ten.py � GitHub, string with all even indexed characters in each word upper cased, and all odd character is lower case when it says even is upper case and 0th index is even [code]import java.io.*; import java.util.*; class Alt { public static void main(String args[])throws IOException { Scanner in = new Scanner(System.in); System.out