How to find the middle character of a string in python

Last update on August 19 2022 21:50:48 (UTC/GMT +8 hours)

Python Basic - 1: Exercise-93 with Solution

Write a Python program to find the middle character(s) of a given string. If the length of the string is even return the two middle characters. If the length of the string is odd, return the middle character.

Sample Solution:

Python Code:

def middle_char(txt):
  return txt[(len(txt)-1)//2:(len(txt)+2)//2]
text = "Python"
print("Original string: ",text)
print("Middle character(s) of the said string: ",middle_char(text))
text = "PHP"
print("Original string: ",text)
print("Middle character(s) of the said string: ",middle_char(text))
text = "Java"
print("Original string: ",text)
print("Middle character(s) of the said string: ",middle_char(text))

Sample Output:

Original string:  Python
Middle character(s) of the said string:  th
Original string:  PHP
Middle character(s) of the said string:  H
Original string:  Java
Middle character(s) of the said string:  av

Pictorial Presentation:

How to find the middle character of a string in python

Flowchart:

How to find the middle character of a string in python

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to compute cumulative sum of numbers of a given list.
Next: Write a Python program to find the largest product of the pair of adjacent elements from a given list of integers.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given string str, the task is to print the middle character of a string. If the length of the string is even, then there would be two middle characters, we need to print the second middle character.

    Examples:

    Input: str = “Java”
    Output: v
    Explanation: 
    The length of the given string is even. 
    Therefore, there would be two middle characters ‘a’ and ‘v’, we print the second middle character.

    Input: str = “GeeksForGeeks”
    Output: o
    Explanation: 
    The length of the given string is odd. 
    Therefore, there would be only one middle character, we print that middle character.

    Approach:

    1. Get the string whose middle character is to be found.
    2. Calculate the length of the given string.
    3. Finding the middle index of the string.
    4. Now, print the middle character of the string at index middle using function charAt() in Java.

    Below is the implementation of the above approach:

    C++

    #include

    using namespace std;

    void  printMiddleCharacter(string str)

    {

        int len = str.size();

        int middle = len / 2;

        cout << str[middle];

    }

    int main()

    {

        string str = "GeeksForGeeks";

        printMiddleCharacter(str);

        return 0;

    }

    Java

    class GFG {

        public static void

        printMiddleCharacter(String str)

        {

            int len = str.length();

            int middle = len / 2;

            System.out.println(str.charAt(middle));

        }

        public static void

        main(String args[])

        {

            String str = "GeeksForGeeks";

            printMiddleCharacter(str);

        }

    }

    Python3

    def printMiddleCharacter(str):

        length = len(str);

        middle = length // 2;

        print(str[middle]);

    str = "GeeksForGeeks";

    printMiddleCharacter(str);

    C#

    using System;

    class GFG{

    public static void printMiddlechar(String str)

    {

        int len = str.Length;

        int middle = len / 2;

        Console.WriteLine(str[middle]);

    }

    public static void Main(String []args)

    {

        String str = "GeeksForGeeks";

        printMiddlechar(str);

    }

    }

    Javascript

    Time Complexity: O(1) 
    Auxiliary Space: O(1)
     


    How do you find the middle character in a string?

    Approach:.
    Get the string whose middle character is to be found..
    Calculate the length of the given string..
    Finding the middle index of the string..
    Now, print the middle character of the string at index middle using function charAt() in Java..

    How do you find the mid value of a string in Python?

    If there is no middle letter, your function should return the empty string. For example, mid("abc") should return "b" and mid("aaaa") should return "".

    How do you find the middle of a word in Python?

    Use len , which finds the length of the object..
    {0:...} tells format to replace replace itself with the first argument, '^' ..
    {1} gets replaced by the second argument, len(text) ..
    ^{1} tells format to center the text, and make the total width equal to len(text) ..

    How do I find a character in a string python?

    Find Character in a String in Python.
    Use the find() Function to Find the Position of a Character in a String..
    Use the rfind() Function to Find the Position of a Character in a String..
    Use the index() Function to Find the Position of a Character in a String..
    Use the for Loop to Find the Position of a Character in a String..