Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String comparison in Python that is case-insensitive for first letter

I need to match the following string File system full. The problem is Starting F can be lowercase or capital. How can I do this in Python when string comparisons are usually case-sensitive?

like image 792
Sandeep Krishnan Avatar asked Apr 23 '26 05:04

Sandeep Krishnan


2 Answers

I'll be providing boolean indicators for you to play around with (rather than actual if blocks for the sake of conciseness.

Using Regex:

import re
bool(re.match('[F|f]',<your string>)) #if it matched, then it's true.  Else, false.

if the string could be anywhere in your output (I assume string)

import re
bool(re.search('[F|f]ile system full',<your string>))

Other options:

checking for 'f' and 'F'

<your string>[0] in ('f','F')

<your string>.startswith('f') or <your string>.startswith('F')

And there's the previously suggested lower method:

<your string>.lower() == 'f'
like image 161
Snakes and Coffee Avatar answered Apr 24 '26 19:04

Snakes and Coffee


You can lower your string before comparing it.

like image 21
Scharron Avatar answered Apr 24 '26 19:04

Scharron