Python Forum
Display middle character/s
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Display middle character/s
#3
(Feb-01-2018, 05:27 AM)wavic Wrote: If the string length is even string[len(string)//2 - 1] is the first of the middle characters and string[len(string)//2] is the second. Just calculate the index.

Thank you for your help, took a bit of tinkering and a lot of loud grunts but I did it. This is what if finally figured out.

## Middle-Character
def MC():
  mc = input('Enter String: ')
  if len(mc) % 2 == 0:
#even  
    print(mc[len(mc) // 2 - 1] + mc[len(mc) // 2])
  else: 
#odd
    print(mc[len(mc) // 2])
Reply


Messages In This Thread
Display middle character/s - by jmcatena91 - Jan-31-2018, 10:37 PM
RE: Display middle character/s - by wavic - Feb-01-2018, 05:27 AM
RE: Display middle character/s - by jmcatena91 - Feb-01-2018, 11:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding and storing all string with character A at middle position Pippi 2 2,754 Jan-20-2019, 08:23 AM
Last Post: Pippi
  Unexpected character after line continuation character joshyb123 5 10,748 Sep-05-2018, 08:08 AM
Last Post: perfringo
  unexpected character after line continuation character error newbie 10 14,864 Aug-09-2018, 06:07 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020