{"id":20216,"date":"2021-09-09T19:57:26","date_gmt":"2021-09-09T14:27:26","guid":{"rendered":"https:\/\/python-programs.com\/?p=20216"},"modified":"2021-11-22T18:36:25","modified_gmt":"2021-11-22T13:06:25","slug":"python-program-to-convert-alternate-characters-to-capital-letters","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-convert-alternate-characters-to-capital-letters\/","title":{"rendered":"Python Program to Convert Alternate Characters to Capital Letters"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Check Two Matrix are Equal or Not<\/a><\/p>\n Given a string, and the task is to convert the alternate characters to capital letters for a given string in python<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to convert the alternate characters to capital letters for a given string in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Find the best practical and ready-to-use Python Programming Examples<\/a> that you can simply run on a variety of platforms and never stop learning.<\/p>\n In the previous article, we have discussed Python Program to Check Two Matrix are Equal or Not Given a string, and the task is to convert the alternate characters to capital letters for a given string in python Examples: Example1: Input: Given String = “good morning btechgeeks” Output: The given string after converting alternate characters …<\/p>\nGiven String = \"good morning btechgeeks\"<\/pre>\n
The given string after converting alternate characters into capital letters :\r\nGoOd MoRnInG bTeChGeEkS<\/pre>\n
Given String = \"hello this is btechgeeks\"<\/pre>\n
The given string after converting alternate characters into capital letters :\r\nHeLlO tHiS iS bTeChGeEkS<\/pre>\n
Program to Convert Alternate Characters to Capital Letters in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngven_str = \"good morning btechgeeks\"\r\n# Take a variable say k and initialize its value to zero.\r\nk = 0\r\n# Take a new empty string and store it in another variable.\r\nmodified_strng = \"\"\r\n# Calculate the length of the given string using the len() function and store it in\r\n# another variable.\r\nstr_lengt = len(gven_str)\r\n# Loop till the length of the given string using the for loop.\r\nfor itr in range(str_lengt):\r\n # Check if the above-initialized k value modulus 2 is equal to zero using the if\r\n # conditional statement.\r\n if k % 2 == 1:\r\n # If the Statement is true, Check if the character which is present at the iterator\r\n # value of the given string is greater than or equal to 'A' and less than or equal to 'Z' using the\r\n # if conditional statement.\r\n if gven_str[itr] >= 'A' and gven_str[itr] <= 'Z':\r\n # If it is true, calculate the ASCII value of the character of the given string which is\r\n # present at the iterator value index using the ord() function, and add 32 to it\r\n # Find the character of the above ASCII value using the chr() function.\r\n charctr = chr(ord(gven_str[itr])+32)\r\n # Concatenate the above character to the modified string.\r\n modified_strng = modified_strng+charctr\r\n else:\r\n # If the statement is False, then concatenate the character which is present at the\r\n # iterator value of the given string to the modified string.\r\n modified_strng = modified_strng+gven_str[itr]\r\n else:\r\n # Else, check if the character which is present at the iterator value of the given string is greater\r\n # than or equal to 'a' and less than or equal to 'z' using the if conditional statement.\r\n if gven_str[itr] >= 'a' and gven_str[itr] <= 'z':\r\n # If it is true, calculate the ASCII value of the character of the given string which\r\n # is present at the iterator value index using the ord() function, and subtract\r\n # 32 from it.\r\n # Find the character of the above ASCII value using the chr() function.\r\n charctr = chr(ord(gven_str[itr])-32)\r\n # Concatenate the above character to the modified string.\r\n modified_strng = modified_strng+charctr\r\n else:\r\n # If the statement is False, then concatenate the character which is present at the\r\n # iterator value of the given string to the modified string.\r\n modified_strng = modified_strng+gven_str[itr]\r\n # Check if the character present at the iterator value is equal to space using the if\r\n # conditional statement.\r\n\r\n if gven_str[itr] == ' ':\r\n # If it is true, then continue.\r\n continue\r\n # Increment the value of k by 1 and store it in the same variable k.\r\n k = k+1\r\n# Print the modified string to get the given string after converting alternate characters\r\n# into capital letters.\r\nprint(\"The given string after converting alternate characters into capital letters :\")\r\nprint(modified_strng)\r\n<\/pre>\n
The given string after converting alternate characters into capital letters :\r\nGoOd MoRnInG bTeChGeEkS<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and \r\n# store it in a variable.\r\ngven_str = input(\"Enter some Random String = \")\r\n# Take a variable say k and initialize its value to zero.\r\nk = 0\r\n# Take a new empty string and store it in another variable.\r\nmodified_strng = \"\"\r\n# Calculate the length of the given string using the len() function and store it in\r\n# another variable.\r\nstr_lengt = len(gven_str)\r\n# Loop till the length of the given string using the for loop.\r\nfor itr in range(str_lengt):\r\n # Check if the above-initialized k value modulus 2 is equal to zero using the if\r\n # conditional statement.\r\n if k % 2 == 1:\r\n # If the Statement is true, Check if the character which is present at the iterator\r\n # value of the given string is greater than or equal to 'A' and less than or equal to 'Z' \r\n # using the if conditional statement.\r\n if gven_str[itr] >= 'A' and gven_str[itr] <= 'Z':\r\n # If it is true, calculate the ASCII value of the character of the given string which is\r\n # present at the iterator value index using the ord() function, and add 32 to it\r\n # Find the character of the above ASCII value using the chr() function.\r\n charctr = chr(ord(gven_str[itr])+32)\r\n # Concatenate the above character to the modified string.\r\n modified_strng = modified_strng+charctr\r\n else:\r\n # If the statement is False, then concatenate the character which is present at the\r\n # iterator value of the given string to the modified string.\r\n modified_strng = modified_strng+gven_str[itr]\r\n else:\r\n # Else, check if the character which is present at the iterator value of the given string \r\n # is greater than or equal to 'a' and less than or equal to 'z' using the if\r\n # conditional statement.\r\n if gven_str[itr] >= 'a' and gven_str[itr] <= 'z':\r\n # If it is true, calculate the ASCII value of the character of the given string which\r\n # is present at the iterator value index using the ord() function, and subtract\r\n # 32 from it.\r\n # Find the character of the above ASCII value using the chr() function.\r\n charctr = chr(ord(gven_str[itr])-32)\r\n # Concatenate the above character to the modified string.\r\n modified_strng = modified_strng+charctr\r\n else:\r\n # If the statement is False, then concatenate the character which is present at the\r\n # iterator value of the given string to the modified string.\r\n modified_strng = modified_strng+gven_str[itr]\r\n # Check if the character present at the iterator value is equal to space using the if\r\n # conditional statement.\r\n\r\n if gven_str[itr] == ' ':\r\n # If it is true, then continue.\r\n continue\r\n # Increment the value of k by 1 and store it in the same variable k.\r\n k = k+1\r\n# Print the modified string to get the given string after converting alternate characters\r\n# into capital letters.\r\nprint(\"The given string after converting alternate characters into capital letters :\")\r\nprint(modified_strng)\r\n<\/pre>\n
Enter some Random String = hello this is btechgeeks\r\nThe given string after converting alternate characters into capital letters :\r\nHeLlO tHiS iS bTeChGeEkS<\/pre>\n
\n