Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to compare string
#1
I need help to compare string

Let's say I am getting string continousely from input
input message may be following

string = ";Day1=13/10/18;Day2=14= Shift;Time11=15:17;PRICE=10"

strong = ";Day1=13/10/18;Day2=14=A;Time11=15:17;PRICE=10" #no change in strig
string = ";Day1=14/10/18;Day2=14=A;Time11=15:17;PRICE=10" #only date change
string ";Day1=13/10/18;Day2=14=B;Time11=15:17;PRICE=10" #section change
string = ";Day1=13/10/18;Day2=14=A;Time11=15:17;PRICE=12" #price change

if any change happen in input like date change then print date change, if section change then print section change if price change print price change

How to make script
Reply
#2
Use the split method with the semi-colon to separate the parts. Compare each part to the previous corresponding part (zip and a for loop would make this really easy). If they are different print. Store the current parts as the new previous parts and repeat.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
The difflib module can be useful for various tasks comparing two strings or files. It is very similar to git/mercurual.
https://pymotw.com/3/difflib/
Recommended Tutorials:
Reply
#4
(Jan-17-2019, 06:08 PM)ichabod801 Wrote: ";Day1=13/10/18;Day2=14= Shift;Time11=15:17;PRICE=10"
done here
# Input string.
Input_string = ";Day1=13/10/18;Day2=14= Shift;Time11=15:17;PRICE=10"

# Separate on comma.
Separate_comma = Input_string.split(";")

# Loop and print each city name.
for string in Separate_comma:
    print(string)
output
Day1=13/10/18
Day2=14= Shift
Time11=15:17
PRICE=10
How to compare present and previous date ?
How to compare p and previous time ?
How to compare present and previous price ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to compare string values in an if statement israelsattleen 1 541 Jul-08-2023, 03:49 PM
Last Post: deanhystad
  how to compare some string with the database columns nick123 1 1,886 Apr-21-2019, 05:27 PM
Last Post: ichabod801
  Does Python 3.x have a built-in byte string compare function? Raptor88 2 16,396 Feb-18-2017, 10:44 AM
Last Post: Raptor88

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020