{"id":12436,"date":"2021-09-30T12:30:37","date_gmt":"2021-09-30T07:00:37","guid":{"rendered":"https:\/\/python-programs.com\/?p=12436"},"modified":"2021-11-22T18:34:38","modified_gmt":"2021-11-22T13:04:38","slug":"python-program-for-printing-odd-and-even-letters-of-a-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-printing-odd-and-even-letters-of-a-string\/","title":{"rendered":"Python Program for Printing Odd and Even Letters of a String"},"content":{"rendered":"
Strings in Python:<\/strong><\/p>\n String in Python. The string is an immutable sequence data type in Python. It is a string of Unicode letters surrounded by single, double, or triple quotations… This is the second string in the Multi-line format. If a string literal must embed double quotations as part of a string, it must be enclosed in single quotes.<\/p>\n Given the string, the task is to print even and odd index characters of the 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 write a Python Program to print Odd and Even Letters of the given String.<\/p>\n If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list<\/a> available.<\/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 Related Programs<\/strong>:<\/p>\n Strings in Python: String in Python. The string is an immutable sequence data type in Python. It is a string of Unicode letters surrounded by single, double, or triple quotations… This is the second string in the Multi-line format. If a string literal must embed double quotations as part of a string, it must be …<\/p>\nThe given string = BtechGeeks<\/pre>\n
The odd characters in given string = Behek\r\nThe even characters in given string = tcGes<\/pre>\n
The given string = uploadbutton<\/pre>\n
The odd characters in given string = ulabto\r\nThe even characters in given string = podutn<\/pre>\n
Python Program for Printing Odd and Even Letters of a String<\/h2>\n
\n
Method #1: Using For Loop and String Concatenation (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngivenstring = 'BtechGeeks'\r\n# Take two empty strings(say oddChars and evenChars)\r\n# and initialize them with a null string using \"\".\r\nevenChars = \"\"\r\noddChars = \"\"\r\n# Calculate the length of the string using the len() function and store it in a variable.\r\nstrnglen = len(givenstring)\r\n# Loop till the length of the string using For loop.\r\nfor itrvalue in range(strnglen):\r\n # Check if the iterator value is divisible by 2 or not using the If statement.\r\n #\r\n if itrvalue % 2 == 0:\r\n # If it is true then concatenate the string iterator value to the oddChars string\r\n oddChars = oddChars + givenstring[itrvalue]\r\n else:\r\n # Else concatenate the string iterator value to the evenChars string.\r\n evenChars = evenChars + givenstring[itrvalue]\r\nprint('The given string = ', givenstring)\r\n# Print the oddChars string.\r\nprint('The odd characters in given string = ', oddChars)\r\n# Print the evenChars string.\r\nprint('The even characters in given string =', evenChars)\r\n<\/pre>\n
The given string = BtechGeeks\r\nThe odd characters in given string = Behek\r\nThe even characters in given string = tcGes<\/pre>\n
Method #2: Using For Loop and String Concatenation (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngivenstring = input('Enter some random string = ')\r\n# Take two empty strings(say oddChars and evenChars)\r\n# and initialize them with a null string using \"\".\r\nevenChars = \"\"\r\noddChars = \"\"\r\n# Calculate the length of the string using the len() function and store it in a variable.\r\nstrnglen = len(givenstring)\r\n# Loop till the length of the string using For loop.\r\nfor itrvalue in range(strnglen):\r\n # Check if the iterator value is divisible by 2 or not using the If statement.\r\n #\r\n if itrvalue % 2 == 0:\r\n # If it is true then concatenate the string iterator value to the oddChars string\r\n oddChars = oddChars + givenstring[itrvalue]\r\n else:\r\n # Else concatenate the string iterator value to the evenChars string.\r\n evenChars = evenChars + givenstring[itrvalue]\r\nprint('The given string = ', givenstring)\r\n# Print the oddChars string.\r\nprint('The odd characters in given string = ', oddChars)\r\n# Print the evenChars string.\r\nprint('The even characters in given string =', evenChars)\r\n<\/pre>\n
Enter some random string = uploadbutton\r\nThe given string = uploadbutton\r\nThe odd characters in given string = ulabto\r\nThe even characters in given string = podutn<\/pre>\n
\n