The way you compare two string in Python.... | Technical, Finance, Investment Questions

The way you compare two string in Python....

Ayush Raj

2 years ago

To compare for equality, for case sensitive comparisons, Use ‘==‘‘an’ == ‘a’+’n’

For case insensitive comparisons, convert one to lower case before using ‘==‘a=‘Hello’

a.casefold() == ‘hello’

Obviously to check for inequality use !=You can also use >, <, <= and >= to test for relative ordering of strings - so called dictionary ordering.‘aardvark’ <= ‘ambleforth’

WarningDon’t ever use ‘is’ to compare for equality - while it will work for some small strings due to the way that Python currently internally manages memory it won’t work in the general case and using it can cause some subtle bugsNote : An Earlier version of this answer suggested using .lower() - it is better to use .casfold() as above as casefold deals more correctly with none ASCII character sets (i.e. those in use everywhere across the world except English speaking countries). You might think you will only ever run your code in the UK or USA, but you can’t control who else might use your code, and where else they might run it. Time to form good habits.







Ayush Raj

Student at KOLKATA

2 years ago

Featured Blogs

Category: Technology

Author: Namratha Reddy

Posted : 6 months ago

102( 0 Comments )
not found

Category: Technology

Author: Priya Singh

Posted : 7 months ago

287( 0 Comments )

Category: Technology

Author: Shweta Rathod

Posted : 8 months ago

104( 0 Comments )

Category: Technology

Author: Juhi Mehra

Posted : 5 years ago

43( 4 Comments )

Category: Technology

Author: Juhi Mehra

Posted : 7 years ago

99( 1 Comments )
Featured Questions

Category: Technology

Author: Namratha Reddy

Posted : 6 months ago

102( 0 Comments )
not found

Category: Technology

Author: Priya Singh

Posted : 7 months ago

287( 0 Comments )

Category: Technology

Author: Shweta Rathod

Posted : 8 months ago

104( 0 Comments )

Category: Technology

Author: Juhi Mehra

Posted : 5 years ago

43( 4 Comments )

Category: Technology

Author: Juhi Mehra

Posted : 7 years ago

99( 1 Comments )
not found

Category: Investment

Author: Sushmita Pal

Posted : 14 hours ago

0( 0 Comments )
not found

Category: Investment

Author: Sushmita Pal

Posted : 3 days ago

0( 0 Comments )
not found

Category: Investment

Author: Sushmita Pal

Posted : 4 days ago

60( 0 Comments )
not found

Category: Startup

Author: Anushka Trivedi

Posted : 5 days ago

50( 0 Comments )
not found

Category: Investment

Author: Anushka Trivedi

Posted : 5 days ago

32( 0 Comments )
not found

Category: Investment

Author: Anushka Trivedi

Posted : 6 days ago

64( 0 Comments )
not found

Category: Startup

Author: Anushka Trivedi

Posted : 10 days ago

142( 0 Comments )
not found

Category: Startup

Author: Anushka Trivedi

Posted : 13 days ago

122( 0 Comments )
not found

Category: Technology

Author: Sweety Singh

Posted : 14 days ago

173( 0 Comments )
not found

Category: Stocks

Author: Sweety Singh

Posted : 16 days ago

69( 0 Comments )