def longestWord(sentence):
    longest = 0   # Keep track of the longest length
    word = ''     # And the word that corresponds to that length
    for i in sentence.split():
        if len(i) > longest:
            word = i
            longest = len(i)
    return word

>>> s = 'this is a test sentence with some words'
>>> longestWord(s)
'sentence'
Answer from Cory Kramer on Stack Overflow
🌐
Reddit
reddit.com › r/pythonprojects2 › different length string compare print true
r/PythonProjects2 on Reddit: Different length string compare print true
December 23, 2025 - But why would you make the function like that, you already have both lengths so you can just do: def compare_lengths(string1: str, string2: str) -> bool: return len(string1) == len(string2) ...
Discussions

making string comparision in python - Stack Overflow
You can use str.casefold for ... (Python 3.3+ only). Copy>>> 'A'.casefold() 'a' >>> 'A'.casefold() == 'a'.casefold() True ... Sign up to request clarification or add additional context in comments. ... Save this answer. ... Show activity on this post. Former one just compares the length of two strings... More on stackoverflow.com
🌐 stackoverflow.com
python - Fastest way to compare one string of two lengths - Stack Overflow
I'm looking for the fastest way to compare if a string has one of two lengths. This string must be either one letter longer or shorter, and I feel that the if statement below might not be the faste... More on stackoverflow.com
🌐 stackoverflow.com
Compare 2 Strings in Python - Stack Overflow
So, I need to figure out a program that when you input 2 different strings of the same length it will return NOT print the number of differences between the two strings. The order of the characters More on stackoverflow.com
🌐 stackoverflow.com
performance - Python program to take in two strings and print the larger string - Code Review Stack Exchange
What is happening is that (s,s2) ... then compares the two, and because they're within square brackets []; the boolean result is casted to an integer index. Since you can only have a 0 or 1 result, this returns s if it is larger than s2, and vice-versa. EDIT: This returns s if both strings are of equal lengths... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
May 22, 2019
🌐
Rosetta Code
rosettacode.org › wiki › Compare_length_of_two_strings
Compare length of two strings - Rosetta Code
1 week ago - Task Given two strings of different length, determine which string is longer or shorter. Print both strings and their length, one on each line. Print the longer...
🌐
Notes
notes.iamdev.in › home › how to compare the length of two strings in python without using built-in functions
How to Compare the Length of Two Strings in Python Without Using Built-in Functions » Notes
August 10, 2024 - Step 2: We initialize two counters, i and j, to 0. These will keep track of the lengths of strr1 and strr2, respectively. Step 3: We use a for loop to go through each character in strr1. Every time the loop runs, we add 1 to i, effectively counting the characters in the first string. Step 4: We repeat the same process for strr2 using another for loop and increment j for each character. Step 5: Finally, we compare i and j.
🌐
Finxter
blog.finxter.com › home › learn python blog › comparing string lengths in python without built-in functions
Comparing String Lengths in Python Without Built-in Functions - Be on the Right Side of Change
March 6, 2024 - The compare_strings() function then returns the longer string based on these recursive length calculations. Python allows tuple unpacking which we can leverage to simulate simultaneous iteration over two strings.
🌐
Medium
yashvaantlakham73.medium.com › string-comparison-in-python-834418a69686
String Comparison in Python. Unleash the power of precision | by A.I Hub | Medium
October 14, 2024 - Dive into the mechanics behind string comparison and discover how understanding this concept can elevate your python programming to the next level. ... The operators is and is not are used to compare the identity of strings and other objects. They check whether the two strings occupy the same space in memory.
Find elsewhere
🌐
USAVPS
usavps.com › home › python › python tutorial: how to compare string sizes in python?
Python Tutorial: How to Compare String Sizes in Python? - USAVPS
October 24, 2024 - def compare_string_sizes(str1, str2): len1 = len(str1) len2 = len(str2) if len1 > len2: return "The first string is longer." elif len1 < len2: return "The second string is longer." else: return "Both strings are of equal length." result = compare_string_sizes("Hello", "World!") print(result) ...
🌐
CodeScracker
codescracker.com › python › program › python-program-compare-two-strings.htm
Python Program to Compare Two Strings
Similarly, the length of second string gets initialized to lenTwo. For example, if user enters first string as codescracker and second string as same as first. Then the dry run of above program goes like: Through above two statements, lenOne=12 and lenTwo=12
🌐
Delft Stack
delftstack.com › home › howto › python › python compare two strings character by character
How to Compare Two Strings Character by Character in Python | Delft Stack
February 2, 2024 - The compare_strings function begins by checking if either of the input strings is None, and if so, it prints a message indicating that the number of identical characters is zero. Next, it determines the minimum length of the two strings to ensure ...
🌐
Index.dev
index.dev › blog › python-string-comparison-methods
Python String Comparison: 8 Easy Ways You Must Know
May 15, 2025 - While theoretically it has an O(n×m) time complexity (where n is your main string length and m is the substring length). It may seem too complex but Python's implementation is actually quite clever - it uses optimized string search algorithms behind the scenes, so you'll find it's much faster than if you tried to code a substring search from scratch. ... difflib module is invaluable. The SequenceMatcher calculates a similarity ratio that quantifies the closeness between two strings. import difflib from typing import Union, Sequence def fuzzy_compare(s1: str, s2: str, *, autojunk: bool = False) -> float: """ Compute the similarity ratio between two strings using difflib.SequenceMatcher.
🌐
The CoderPedia
thecoderpedia.com › home › python string comparison: a complete guide to compare strings in python
Python String Comparison: A Complete Guide to Compare Strings in Python
August 20, 2021 - Python String Comparison: Strings are the set of characters. In Python, there is no separate Data Type for defining Character. So, String of length 1 can be used as a Character in Python.
🌐
Learn Coding Fast
learncodingfast.com › home › how to compare strings in python – a complete guide
How to compare strings in Python – A complete guide | Learn Coding Fast
October 19, 2020 - Next, mainStr is considered to be smaller than str6 as its first character 'p' is smaller than the first (and only) character of str6 ('z'). ... That’s it. We have now covered all the string comparison operators in Python. Let’s move on to the practice question for today. Today’s practice question requires us to write a function called compareStr() that has two parameters – a and b, both of which are strings.
🌐
Oreate AI
oreateai.com › blog › mastering-string-comparison-in-python-a-comprehensive-guide › 2a51f14b66b9eb476c81466d51b21c2e
Mastering String Comparison in Python: A Comprehensive Guide - Oreate AI Blog
January 15, 2026 - This means that when you compare two strings, Python evaluates them character by character from the beginning until it finds a difference or reaches the end. For instance, consider the simple case of comparing 'apple' and 'banana'. The first characters are compared: 'a' (Unicode 97) versus 'b' (Unicode 98). Since 97 is less than 98, Python concludes that 'apple' is less than 'banana'. But what happens if the strings have different lengths...
🌐
WsCube Tech
wscubetech.com › resources › python › programs › string-comparison
Comparison of Strings in Python (6 Programs)
November 3, 2025 - Master string comparison in Python with 6 easy programs. Follow step-by-step examples with outputs and clear explanations. Read now!
🌐
ReqBin
reqbin.com › code › python › l1pjhl0c › python-compare-strings-example
How do I compare strings in Python?
July 27, 2023 - If the first line is a prefix of the second, then it is less than the second. If two lines start the same, but then differ, then less is the line with the first differing character less.
🌐
Scaler
scaler.com › home › topics › string comparison in python
How to Compare Strings in Python? (With Examples) - Scaler Topics
February 15, 2024 - Here we have implemented the comp_str function to compare two strings. First, the function checks whether the strings are of the same length. If they have different lengths, it returns that the strings are not equal.