Skip to content

chrono208/Mid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Mid

Returns the middle character of a given string.

Overview

The mid() function returns the middle character of a given string.
If the string has an even number of characters, it returns an empty string.
If the string has an odd number of characters, it returns the single character that sits exactly in the middle.

How it works

  • A variable found is initialized as an empty string.
  • The function checks if the length of the word is even (len(word) % 2 == 0):
    • If even, it immediately returns an empty string ("").
  • If odd, it calculates the middle character’s position with:
    • index = len(word)/2 + 0.5 - 1
  • This formula ensures the result is centered for odd-length strings.
  • The function converts the result to an integer and returns the character at that position.

Example

result = mid("testing") print(result)

Output

t

Example 2

result = mid("test") print(result)

Output 2

""

About

Returns the middle character of a given string.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages