Question on Python Function to capaitalize letters at even position

every even letter is uppercase python
alternate letters in uppercase in python
alternate letters in uppercase in c
enumerate python
alternate case javascript
define a function called myfunc that takes in an arbitrary number of arguments and returns a list
spongebob case programming
enumerate text file python

I am able to perform the capitalization function using the below for loop and enumerate function.

wordlist = list(word)
for i,v in enumerate(wordlist):
    if i%2 != 0:
        wordlist[i] = v.upper()
    else:
        wordlist[i] = v.lower()
        word2 = "".join(wordlist)
print(word2)

However when I try to put it into the function in python, I am not able to reproduce the same result as above:

def myfunc(*word):
    wordlist = list(word)
    for i,v in enumerate(wordlist):
        if i%2 != 0:
            wordlist[i] = v.upper()
        else:
            wordlist[i] = v.lower()
            word = "".join(wordlist)
    return (word)

Can anyone help me with my question?

No need to unpack (*) that makes word equal to (word,) i.e. single element tuple:

def myfunc(word):
    wordlist = list(word)
    for i,v in enumerate(wordlist):
        if i%2 != 0:
            wordlist[i] = v.upper()
        else:
            wordlist[i] = v.lower()
            word = "".join(wordlist)
    return (word)

Or easier:

def myfunc(word):
    word=list(word.lower())
    for i,v in enumerate(word):
        if i%2 != 0:
            word[i] = v.upper()
    return ''.join(word)

Better:

def myfunc(word):
    word=list(word.lower())
    word[1::2]=map(str.upper,word[1::2])
    return ''.join(word)

How to uppercase even letter and lowercase odd letter in a string , Some issues in your code: Currently you are returning from the function on the first index itself, i.e index=0 when you do return st.lower() , so the  I'm just beginning to learn Python, so please bear with me. My math knowledge is a little shaky as well. I'm able to capitalize a single word, or first word in a string. What I am having a problem with is i need to capitalize every 3rd letter in the string. I need to do this as a function.

Easier and simpler approach

 def func(word):
        result = ""
        for i in range(len(word)):
            if i % 2 == 0:
                result += word[i].upper()
            else:
                result += word[i]
        return result

Need help with solving a python question - learnprogramming, Question: Define a function called 'myfunc' that takes in a string, and returns a The letter at the start is in an even position, so it should be uppercase (unless  python capitalize first letter only (8 answers) Closed 5 years ago . I need build a function to transform 1st character only, from any word but also this function need address the problem if the 1st character from the word doesn't starts with a character, for example '_sun',' -tree', '2cat' these words need be like _Sun, -Tree, 2Cat.

       word = "prasanna"
       def myfunc(word):
          word=list(word.lower())
          for i,v in enumerate(word):
               if i%2 != 0:
                 word[i] = v.upper()
          print  ''.join(word)
       myfunc(word)    

Program for converting Alternate characters of a string to Upper Case., How can I convert each alternate character of a string to uppercase in Python? Ans: Help() and dir() both functions are accessible from the Python interpreter and used for viewing a consolidated dump of built-in functions. Help() function: The help() function is used to display the documentation string and also facilitates you to see the help related to modules, keywords, attributes, etc.

Python, The problem of case changes a string is quite common and has been the odd character of string to upper case and even positioned characters to lowercase. be performed in brute force method in a way that we iterate through the string  In Python, the capitalize() method converts the first character of a string to capital (uppercase) letter. If the string has its first character as capital, then it returns the original string. If the string has its first character as capital, then it returns the original string.

Convert characters of a string to opposite case, If character in a index is in lower case, then subtract 32 to convert it in upper case​, else add Function to convert characters //Convert lowercase to uppercase. Python: script o makes every other letter of string get capitalized Hi, I'm working on a problem that asks for a script to make every other letter of input string to be capitalized with for loop. ex: input "Hello, World!"

Python Exercise: Calculate the number of upper / lower case letters , Python Functions: Exercise-7 with Solution. Write a Python function that accepts a string and calculate the number of upper case letters and  Python String capitalize() method returns a copy of the string with only its first character capitalized.. Syntax str.capitalize() Parameters. NA. Return Value. string. Example

Comments
  • word in in your function is a tuple because you are using *word and so list(word) is not returning what you expect from it. Do you need to pass multiple words in the function parameter?
  • @AnkitJaiswal Yup, it's returning [word], and default it's (word), because thats basically *args, from this question
  • Why did you use a def myfunc(*word): variable-length argument signature here?
  • Also, your "".join(wordlist) is indented too much.
  • @kindall Oh yeah, why not just at the end