Python Forum
Display middle character/s
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Display middle character/s
#1
OK, so this time I have a question about how to find the middle character/s. My instructions tell me for middle characters is

a. This operation will prompt the user for a choice between 'N' for number and 'S' for string
b. If the string has an odd number of characters
i. Determine the middle character of the string
ii. Display the single middle character to the user.
c. If the string has an even number of characters
i. Determine the middle characters of the string
ii. Display the two middle characters to the user.

I have everything done except this part. I can make it point to the middle characters for odd number numbers and strings but I dont want that, I want it to just display the letter/s and I have no clue where to start with 2 middle character strings. any info would be greatly appreciated!

The Middle Character is def MC and also commented with lable

## multi line comment to easily display the menu
a = '''
**************************
*         menu           *
**************************
* CC - Character Count   *
* RV - Reverse           *
* M5 - Multiply by 5     *
* MC - Middle Characters *
**************************
* EX - Exit              *
**************************
'''
print(a)


## Creating the individual functions for each choice.
## Character Count
def CC():
  cc = input('Enter string: ')
  print("'" + cc + "' is", len(cc), "characters long")
 
## Reverse
def RV():
  rv = input('Enter string: ')
  print(rv[::-1])

## Multiply by 5
def M5():
  m5 = input('choose "N" for number or "S" for string: ')
  if m5.lower() not in ('n', 's'):
    print('That is not a valid option.')
  elif m5.lower() == 'n':
    num = input('Enter a number :')
    print(num, '* 5 equals: ', int(num) * 5)
  elif m5.lower() == 's':
    sstr = input('Enter a string: ')
    print(str(sstr) * 5)

## Middle-Character
def MC():
  mc = input('choose "N" for number or "S" for string: ')
  if mc.lower() not in ('n', 's'):
    print('That is not a valid option.')
  elif mc.lower() == 'n':
## This is used for odd only.    
    text = input('Enter a number :')
    print(text)
    print('{0:^{1}}'.format('^',len(text)))
  elif mc.lower() == 's':
## This is used for odd only.
    text = input('Enter a string: ')
    print(text)
    print('{0:^{1}}'.format('^',len(text)))
 

## My function library
MyFunctions = {
    'cc': CC,
    'rv': RV,
    'm5': M5,
    'mc': MC,
}
 

## Function that actually calls the functions made at the beginning.
def pick_a_function():
    while True:
        func = input("Choose function 'CC', 'RV', 'M5', 'MC' or 'EX': ")
        if func.lower() == 'ex':
          print('Goodbye!')
          break
        elif func.lower() in MyFunctions:
            MyFunctions[func]()
        else:
            print('Invalid function, Try Again')
 
pick_a_function()
Reply
#2
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding and storing all string with character A at middle position Pippi 2 2,710 Jan-20-2019, 08:23 AM
Last Post: Pippi
  Unexpected character after line continuation character joshyb123 5 10,677 Sep-05-2018, 08:08 AM
Last Post: perfringo
  unexpected character after line continuation character error newbie 10 14,757 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