- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if both halves of the string have same set of characters in Python
We can split a long string from the middle and check if the two halves are equal or not. The input string may have an odd or even number of characters. If it has an even number of characters, we divide the two halves by taking half of the length. But if the number of characters is odd then we ignore the middlemost character and then compare the remaining two halves.
In the below program we create the two halves of the input string with above logic and then
Example
from collections import Counter def comparehalves(input_string): str_len = len(input_string) # If number of characyes is odd # ignore the middle character if (str_len % 2 != 0): left = input_string[0:int(str_len / 2)] right = input_string[(int(str_len / 2)) + 1:] else: left = input_string[0:int(str_len / 2)] right = input_string[int(str_len / 2):] # Convert the halves into lists # and sort them l1 = list(left) l1.sort() l2 = list(right) l2.sort() if l1 == l2: print ("Same character in both halves") else: print ("Both halves are different ") in_string = input("Enter String: ") comparehalves(in_string)
Output
Running the above code gives us the following result −
# Run1 Enter String: Tutorials Both halves are different # Run2 Enter String: TutTut Same character in both halves
- Related Articles
- Check if both halves of the string have the same set of characters in Python
- Python program to check if both halves of the string have same set of characters.
- Check if both halves of the string have same set of characters in C#
- Check if both halves of the string have at least one different character in Python
- Check if two String objects have the same value in C#
- Java Program to check if the String contains any character in the given set of characters
- Python - Check if frequencies of all characters of a string are different
- Check if the characters of a given string are in alphabetical order in Python
- Program to check whether String Halves Are Alike in Python
- Check if frequency of all characters can become same by one removal in Python
- Check if a string has all characters with same frequency with one variation allowed in Python
- Python - Check If All the Characters in a String Are Alphanumeric?
- Check if lowercase and uppercase characters are in same order in Python
- Check if characters of one string can be swapped to form other in Python
- Create a new string by alternately combining the characters of two halves of the string in reverse in C++ Program

Advertisements