string-comparison

How to compare two strings by character and print matching positions in python

How to compare two strings by character and print matching positions in python Question: I want to compare two strings by character and then print how many times they have the same character in the same position. If I were to input ‘soon’ and ‘moon’ as the two strings, it would print that they match …

Total answers: 3

Why compare two strings via calculating xor of their characters?

Why compare two strings via calculating xor of their characters? Question: Some time ago I found this function (unfortunately, I don’t remember from where it came from, most likely from some Python framework) that compares two strings and returns a bool value. It’s quite simple to understand what’s going on here. Finding xor between char …

Total answers: 3

When to use which fuzz function to compare 2 strings

When to use which fuzz function to compare 2 strings Question: I am learning fuzzywuzzy in Python. I understand the concept of fuzz.ratio, fuzz.partial_ratio, fuzz.token_sort_ratio and fuzz.token_set_ratio. My question is when to use which function? Do I check the 2 strings’ length first, say if not similar, then rule out fuzz.partial_ratio? If the 2 strings’ …

Total answers: 2

How do I compare version numbers in Python?

How do I compare version numbers in Python? Question: I am walking a directory that contains eggs to add those eggs to the sys.path. If there are two versions of the same .egg in the directory, I want to add only the latest one. I have a regular expression r”^(?P<eggName>w+)-(?P<eggVersion>[d.]+)-.+.egg$ to extract the name and …

Total answers: 16

Checking whether a string starts with XXXX

Checking whether a string starts with XXXX Question: I would like to know how to check whether a string starts with “hello” in Python. In Bash I usually do: if [[ “$string” =~ ^hello ]]; then do something here fi How do I achieve the same in Python? Asked By: John Marston || Source Answers: …

Total answers: 5

Version number comparison in Python

Version number comparison in Python Question: I want to write a cmp-like function that compares two version numbers and returns -1, 0, or 1 based on their compared values. Return -1 if version A is older than version B Return 0 if versions A and B are equivalent Return 1 if version A is newer …

Total answers: 17

Python: Why does ("hello" is "hello") evaluate as True?

Python: Why does ("hello" is "hello") evaluate as True? Question: Why does “hello” is “hello” produce True in Python? I read the following here: If two string literals are equal, they have been put to same memory location. A string is an immutable entity. No harm can be done. So there is one and only …

Total answers: 7

Good Python modules for fuzzy string comparison?

Good Python modules for fuzzy string comparison? Question: I’m looking for a Python module that can do simple fuzzy string comparisons. Specifically, I’d like a percentage of how similar the strings are. I know this is potentially subjective so I was hoping to find a library that can do positional comparisons as well as longest …

Total answers: 12