08-22-2022, 02:10 PM
How to Ignore Case in Strings
<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{"align":"left","id":"580690","slug":"default","valign":"top","ignore":"","reference":"auto","class":"","count":"1","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Problem Formulation and Solution Overview</h2>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">In this article, you’ll learn how to ignore (upper/lower/title) case characters when dealing with Strings in Python.</p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio"><em>The main reason for ignoring the case of a String is to perform accurate String comparisons or to return accurate search results.</em></p>
<p><em>For example, the following three (3) Strings are not identical. If compared with each other, they would return False.</em></p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>when life gives you lemons, make lemonade. <br />WHEN LIFE GIVES YOU LEMONS, MAKE LEMONADE.<br />When Life Gives You Lemons, Make Lemonade.</code></td>
</tr>
</tbody>
</table>
</figure>
<p><em>This is because each character in the <a rel="noreferrer noopener" href="https://theasciicode.com.ar/" data-type="URL" data-id="https://theasciicode.com.ar/" target="_blank">ASCII Table</a> assigns different numeric values for each key or key combination on the keyboard.</em></p>
<p><em>This article outlines various ways to ignore the case of Strings.</em></p>
<hr class="wp-block-separator has-alpha-channel-opacity wp-embed-aspect-16-9 wp-has-aspect-ratio" />
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background"><em><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How would we write code to compare Strings?</em></p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">We can accomplish this task by one of the following options:</p>
<ul type="video" class="wp-embed-aspect-16-9 wp-has-aspect-ratio">
<li><strong>Method 1</strong>: Use a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-lower/" data-type="URL" data-id="https://blog.finxter.com/python-string-lower/" target="_blank"><code>lower()</code></a> and a <a rel="noreferrer noopener" href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" data-type="URL" data-id="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" target="_blank"><code>lambda</code></a></li>
<li><strong>Method 2</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-upper/" data-type="URL" data-id="https://blog.finxter.com/python-string-upper/" target="_blank"><code>upper()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank"><code>List Comprehension</code></a> </li>
<li><strong>Method 3</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-title/" data-type="URL" data-id="https://blog.finxter.com/python-string-title/" target="_blank"><code>title()</code></a></li>
<li><strong>Method 4</strong>: Use <a rel="noreferrer noopener" href="https://docs.python.org/3/library/stdtypes.html" data-type="URL" data-id="https://docs.python.org/3/library/stdtypes.html" target="_blank"><code>casefold()</code></a></li>
<li><strong>Method 5</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-lower/" data-type="URL" data-id="https://blog.finxter.com/python-string-lower/" target="_blank"><code>lower()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary-comprehension/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary-comprehension/" target="_blank">Dictionary Comprehension</a></li>
<li><strong>Bonus</strong>: Convert CSV of Full Names to title case</li>
</ul>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 1: Use lower() and a lambda</h2>
<p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-lower/" data-type="URL" data-id="https://blog.finxter.com/python-string-lower/" target="_blank"><code>lower()</code></a> and a <a rel="noreferrer noopener" href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" data-type="URL" data-id="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" target="_blank"><code>lambda</code></a> to convert a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> of Strings to lower case to search for an Employee.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">staff = ['Andy', 'MAci', 'SteVe', 'DarCY', 'Harvey']
staff_lower = list((map(lambda x: x.lower(), staff)))
print(staff_lower)</pre>
<p>Above declares a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> of Rivers Clothing Employees. If we were searching for an Employee, such as <code>Darcy</code>, using this <a href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank" rel="noreferrer noopener">List</a>, no results would return as <code>Darcy </code>and <code>DarCY</code> are not the same. Let’s fix this issue.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2139.png" alt="ℹ" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Info</strong>: A <a href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" data-type="URL" data-id="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/"><code>l</code></a><code><a rel="noreferrer noopener" href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" data-type="URL" data-id="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" target="_blank">a</a></code><a href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" data-type="URL" data-id="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/"><code>mbda</code></a> function is a <a href="https://blog.finxter.com/python-one-line-x/" data-type="post" data-id="10612" target="_blank" rel="noreferrer noopener">one-liner</a> that, in this case, converts each <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> element in <code>staff</code> to lower case. Then, converts to a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-map/" target="_blank"><code>map()</code></a> object and back to a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"></a><a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a>. The output saves to <code>staff_lower</code> and is output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>['andy', 'maci', 'steve', 'darcy', 'harvey']</code></td>
</tr>
</tbody>
</table>
</figure>
<p>If a comparison was made now, it would find the correct Employee.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">if ('Darcy'.lower() == staff_lower[3]): print('Match Found!')</pre>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td> Match Found!</td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-ignore-case-in-strings/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FkBg4n52XoUQ%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 2: Use upper() and List Comprehension</h2>
<p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-upper/" data-type="URL" data-id="https://blog.finxter.com/python-string-upper/" target="_blank"><code>upper()</code></a> and a <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank">list comprehension</a> to convert a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> of Strings to upper case and search for an Employee.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">brands = ['ChaNnel', 'CLINique', 'DIOR', 'Lancome', 'MurAD']
brands_upper = [x.upper() for x in brands]
print(brands_upper)</pre>
<p>Above declares a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank">List</a> of the Top five (5) Beauty Brands in the world. If we were searching for a Company, such as <code>Channel</code>, using this <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a>, no results would return as <code><code>Channel</code></code> and <code>ChaNnel</code> are not the same. Let’s fix this issue.</p>
<p>A<a href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" data-type="URL" data-id="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/"> </a><a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank">List Comprehension</a> is called and converts each element in <code>brands</code> to upper case. The results are saved to <code>brands_upper</code> and output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code> ['CHANEL', 'CLINIQUE', 'DIOR', 'LANCOME', 'MURAD']</code></td>
</tr>
</tbody>
</table>
</figure>
<p>If a comparison was made now, it would find the correct Brand.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">my_fav = 'Murad'
if (my_fav.upper() == brands_upper[4]): print(f'{my_fav} Found!')</pre>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>Murad Found!</td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-ignore-case-in-strings/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FaBC0VhpXkOQ%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 3: Use title()</h2>
<p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-title/" data-type="URL" data-id="https://blog.finxter.com/python-string-title/" target="_blank"><code>title()</code></a> to convert each word in a String to upper case. Great use of this is to convert a Full Name to title case. This ensures both the First, Middle and Last Names are as expected.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">full_name = 'jessica a. barker'.title()
print(full_name)</pre>
<p>Above accepts a string, appends the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-title/" data-type="URL" data-id="https://blog.finxter.com/python-string-title/" target="_blank"><code>title()</code></a> method to the string, saves it to <code>full_name</code> and outputs to the terminal.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Jessica A. Barker</pre>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-ignore-case-in-strings/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F-UkcLQxzPA4%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 4: Use casefold() and a Dictionary</h2>
<p class="has-global-color-8-background-color has-background"> This method uses <a rel="noreferrer noopener" href="https://docs.python.org/3/library/stdtypes.html" data-type="URL" data-id="https://docs.python.org/3/library/stdtypes.html" target="_blank"><code>casefold()</code></a> to convert a String to lower case. This method does more than <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-lower/" data-type="URL" data-id="https://blog.finxter.com/python-string-lower/" target="_blank"><code>lower()</code></a>: it uses <a rel="noreferrer noopener" href="https://www.unicode.org/versions/Unicode14.0.0/ch03.pdf#G53253" data-type="URL" data-id="https://www.unicode.org/versions/Unicode14.0.0/ch03.pdf#G53253" target="_blank">Unicode Case Folding Rules</a>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">phrase = 'Nichts ist unmöglich für den Unwilligen!'
cfold = phrase.casefold()
print(cfold)</pre>
<p>Above creates a quote in German and saves it to <code>phrase</code>. This ensures all characters are correctly converted to lower case, thus making a caseless match.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>nichts ist unmöglich für den unwilligen!</code></td>
</tr>
</tbody>
</table>
</figure>
<p>What does this say?</p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 5: Use lower() and Dictionary Comprehension</h2>
<p class="has-global-color-8-background-color has-background">This method uses Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-lower/" data-type="URL" data-id="https://blog.finxter.com/python-string-lower/" target="_blank"><code>lower()</code></a> to convert <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank">Dictionary</a> values into lower case using <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary-comprehension/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary-comprehension/" target="_blank">Dictionary Comprehension</a>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">employees = {'Sandy': 'Coder', 'Kevin': 'Network Specialist', 'Amy': 'Designer'}
result = {k: v.lower() for k, v in employees.items()}
print(result)</pre>
<p>Above creates a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank">Dictionary</a> containing Rivers Employees Names and associated Job Title. This saves to <code>employees</code>.</p>
<p>Next, <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary-comprehension/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary-comprehension/" target="_blank">Dictionary Comprehension</a> is used to loop through the values in the <em>key:value</em> <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank">Dictionary</a> pairs and convert the values to lower case. The output saves to <code>result</code> and is output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code> {'Sandy': 'coder', 'Kevin': 'network specialist', 'Amy': 'designer'}</code></td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-ignore-case-in-strings/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FTlEC5Jx72Uc%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Bonus: CSV Column to title() case</h2>
<p class="has-global-color-8-background-color has-background">In this Bonus section, a <a href="https://blog.finxter.com/read-a-csv-file-to-a-pandas-dataframe/" data-type="post" data-id="440655" target="_blank" rel="noreferrer noopener">CSV file</a> you are working with contains a column of Full Names. You want to ensure all data entered in this column is in <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-title/" data-type="URL" data-id="https://blog.finxter.com/python-string-title/" target="_blank"><code>title()</code></a> case. This can be accomplished by running the following code.</p>
<p>Contents of <code>finxter_users.csv</code> file</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>FID</td>
<td> Full_Name</td>
</tr>
<tr>
<td>30022145</td>
<td>sally jenkins</td>
</tr>
<tr>
<td>30022192</td>
<td>joyce hamilton</td>
</tr>
<tr>
<td>30022331</td>
<td>allan thompson</td>
</tr>
<tr>
<td>30022345</td>
<td>harry stiller</td>
</tr>
<tr>
<td>30022157</td>
<td>ben hawkins</td>
</tr>
</tbody>
</table>
</figure>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd users = pd.read_csv('finxter_users.csv')
users['Full_Name'] = users['Full_Name'].str.title()
print(users)</pre>
<p>Above imports the <a href="https://blog.finxter.com/category/pandas-library/" data-type="URL" data-id="https://blog.finxter.com/category/pandas-library/" target="_blank" rel="noreferrer noopener">Pandas</a> library. For installation instructions, click <a href="https://blog.finxter.com/how-to-install-pandas-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-install-pandas-in-python/" target="_blank" rel="noreferrer noopener">here</a>.</p>
<p>Next, the <code>finxter_users.csv</code> file (contents shown above) is read and saved to the DataFrame <code>users</code>.</p>
<p>The highlighted line directly accesses all values in the <code>users['Full_Name']</code> column and converts each entry to <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-title/" data-type="URL" data-id="https://blog.finxter.com/python-string-title/" target="_blank"><code>title()</code></a> case. This saves back to <code>users['Full_Name']</code> and is output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td></td>
<td>FID</td>
<td> Full_Name</td>
</tr>
<tr>
<td>0</td>
<td>30022145</td>
<td>Sally Jenkins</td>
</tr>
<tr>
<td>1</td>
<td>30022192</td>
<td>Joyce Hamilton</td>
</tr>
<tr>
<td>2</td>
<td>30022331</td>
<td>Allan Thompson</td>
</tr>
<tr>
<td>3</td>
<td>30022345</td>
<td>Harry Stiller</td>
</tr>
<tr>
<td>4</td>
<td>30022157</td>
<td>Ben Hawkins</td>
</tr>
</tbody>
</table>
</figure>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Note</strong>: Don’t forget to save the DataFrame to keep the changes.</p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-ignore-case-in-strings/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F-JKVy_HliQE%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio" id="block-323ff1c3-82fc-4193-9c8d-125f5d3c2efb">Summary</h2>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio" id="block-ebf714b3-b2f5-494e-bcd5-0cd3b3709ed7">These six (6) methods of ignoring cases in Strings should give you enough information to select the best one for your coding requirements.</p>
<h2>Programmer Humor</h2>
<p class="has-global-color-8-background-color has-background"><em><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2753.png" alt="❓" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: Why do programmers always mix up Halloween and Christmas?<br /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2757.png" alt="❗" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Answer</strong>: Because Oct 31 equals Dec 25.</em></p>
<p><em>(If you didn’t get this, read our articles on the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-oct-function/" data-type="post" data-id="24365" target="_blank"><code>oct()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-int-function/" data-type="URL" data-id="https://blog.finxter.com/python-int-function/" target="_blank"><code>int()</code></a> Python built-in functions!)</em></p>
</p></p>
</div>
https://www.sickgaming.net/blog/2022/08/...n-strings/
<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{"align":"left","id":"580690","slug":"default","valign":"top","ignore":"","reference":"auto","class":"","count":"1","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Problem Formulation and Solution Overview</h2>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">In this article, you’ll learn how to ignore (upper/lower/title) case characters when dealing with Strings in Python.</p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio"><em>The main reason for ignoring the case of a String is to perform accurate String comparisons or to return accurate search results.</em></p>
<p><em>For example, the following three (3) Strings are not identical. If compared with each other, they would return False.</em></p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>when life gives you lemons, make lemonade. <br />WHEN LIFE GIVES YOU LEMONS, MAKE LEMONADE.<br />When Life Gives You Lemons, Make Lemonade.</code></td>
</tr>
</tbody>
</table>
</figure>
<p><em>This is because each character in the <a rel="noreferrer noopener" href="https://theasciicode.com.ar/" data-type="URL" data-id="https://theasciicode.com.ar/" target="_blank">ASCII Table</a> assigns different numeric values for each key or key combination on the keyboard.</em></p>
<p><em>This article outlines various ways to ignore the case of Strings.</em></p>
<hr class="wp-block-separator has-alpha-channel-opacity wp-embed-aspect-16-9 wp-has-aspect-ratio" />
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background"><em><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How would we write code to compare Strings?</em></p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">We can accomplish this task by one of the following options:</p>
<ul type="video" class="wp-embed-aspect-16-9 wp-has-aspect-ratio">
<li><strong>Method 1</strong>: Use a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-lower/" data-type="URL" data-id="https://blog.finxter.com/python-string-lower/" target="_blank"><code>lower()</code></a> and a <a rel="noreferrer noopener" href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" data-type="URL" data-id="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" target="_blank"><code>lambda</code></a></li>
<li><strong>Method 2</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-upper/" data-type="URL" data-id="https://blog.finxter.com/python-string-upper/" target="_blank"><code>upper()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank"><code>List Comprehension</code></a> </li>
<li><strong>Method 3</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-title/" data-type="URL" data-id="https://blog.finxter.com/python-string-title/" target="_blank"><code>title()</code></a></li>
<li><strong>Method 4</strong>: Use <a rel="noreferrer noopener" href="https://docs.python.org/3/library/stdtypes.html" data-type="URL" data-id="https://docs.python.org/3/library/stdtypes.html" target="_blank"><code>casefold()</code></a></li>
<li><strong>Method 5</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-lower/" data-type="URL" data-id="https://blog.finxter.com/python-string-lower/" target="_blank"><code>lower()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary-comprehension/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary-comprehension/" target="_blank">Dictionary Comprehension</a></li>
<li><strong>Bonus</strong>: Convert CSV of Full Names to title case</li>
</ul>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 1: Use lower() and a lambda</h2>
<p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-lower/" data-type="URL" data-id="https://blog.finxter.com/python-string-lower/" target="_blank"><code>lower()</code></a> and a <a rel="noreferrer noopener" href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" data-type="URL" data-id="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" target="_blank"><code>lambda</code></a> to convert a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> of Strings to lower case to search for an Employee.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">staff = ['Andy', 'MAci', 'SteVe', 'DarCY', 'Harvey']
staff_lower = list((map(lambda x: x.lower(), staff)))
print(staff_lower)</pre>
<p>Above declares a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> of Rivers Clothing Employees. If we were searching for an Employee, such as <code>Darcy</code>, using this <a href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank" rel="noreferrer noopener">List</a>, no results would return as <code>Darcy </code>and <code>DarCY</code> are not the same. Let’s fix this issue.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2139.png" alt="ℹ" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Info</strong>: A <a href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" data-type="URL" data-id="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/"><code>l</code></a><code><a rel="noreferrer noopener" href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" data-type="URL" data-id="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" target="_blank">a</a></code><a href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" data-type="URL" data-id="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/"><code>mbda</code></a> function is a <a href="https://blog.finxter.com/python-one-line-x/" data-type="post" data-id="10612" target="_blank" rel="noreferrer noopener">one-liner</a> that, in this case, converts each <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> element in <code>staff</code> to lower case. Then, converts to a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-map/" target="_blank"><code>map()</code></a> object and back to a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"></a><a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a>. The output saves to <code>staff_lower</code> and is output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>['andy', 'maci', 'steve', 'darcy', 'harvey']</code></td>
</tr>
</tbody>
</table>
</figure>
<p>If a comparison was made now, it would find the correct Employee.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">if ('Darcy'.lower() == staff_lower[3]): print('Match Found!')</pre>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td> Match Found!</td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-ignore-case-in-strings/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FkBg4n52XoUQ%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 2: Use upper() and List Comprehension</h2>
<p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-upper/" data-type="URL" data-id="https://blog.finxter.com/python-string-upper/" target="_blank"><code>upper()</code></a> and a <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank">list comprehension</a> to convert a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> of Strings to upper case and search for an Employee.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">brands = ['ChaNnel', 'CLINique', 'DIOR', 'Lancome', 'MurAD']
brands_upper = [x.upper() for x in brands]
print(brands_upper)</pre>
<p>Above declares a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank">List</a> of the Top five (5) Beauty Brands in the world. If we were searching for a Company, such as <code>Channel</code>, using this <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a>, no results would return as <code><code>Channel</code></code> and <code>ChaNnel</code> are not the same. Let’s fix this issue.</p>
<p>A<a href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" data-type="URL" data-id="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/"> </a><a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank">List Comprehension</a> is called and converts each element in <code>brands</code> to upper case. The results are saved to <code>brands_upper</code> and output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code> ['CHANEL', 'CLINIQUE', 'DIOR', 'LANCOME', 'MURAD']</code></td>
</tr>
</tbody>
</table>
</figure>
<p>If a comparison was made now, it would find the correct Brand.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">my_fav = 'Murad'
if (my_fav.upper() == brands_upper[4]): print(f'{my_fav} Found!')</pre>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>Murad Found!</td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-ignore-case-in-strings/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FaBC0VhpXkOQ%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 3: Use title()</h2>
<p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-title/" data-type="URL" data-id="https://blog.finxter.com/python-string-title/" target="_blank"><code>title()</code></a> to convert each word in a String to upper case. Great use of this is to convert a Full Name to title case. This ensures both the First, Middle and Last Names are as expected.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">full_name = 'jessica a. barker'.title()
print(full_name)</pre>
<p>Above accepts a string, appends the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-title/" data-type="URL" data-id="https://blog.finxter.com/python-string-title/" target="_blank"><code>title()</code></a> method to the string, saves it to <code>full_name</code> and outputs to the terminal.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Jessica A. Barker</pre>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-ignore-case-in-strings/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F-UkcLQxzPA4%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 4: Use casefold() and a Dictionary</h2>
<p class="has-global-color-8-background-color has-background"> This method uses <a rel="noreferrer noopener" href="https://docs.python.org/3/library/stdtypes.html" data-type="URL" data-id="https://docs.python.org/3/library/stdtypes.html" target="_blank"><code>casefold()</code></a> to convert a String to lower case. This method does more than <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-lower/" data-type="URL" data-id="https://blog.finxter.com/python-string-lower/" target="_blank"><code>lower()</code></a>: it uses <a rel="noreferrer noopener" href="https://www.unicode.org/versions/Unicode14.0.0/ch03.pdf#G53253" data-type="URL" data-id="https://www.unicode.org/versions/Unicode14.0.0/ch03.pdf#G53253" target="_blank">Unicode Case Folding Rules</a>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">phrase = 'Nichts ist unmöglich für den Unwilligen!'
cfold = phrase.casefold()
print(cfold)</pre>
<p>Above creates a quote in German and saves it to <code>phrase</code>. This ensures all characters are correctly converted to lower case, thus making a caseless match.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>nichts ist unmöglich für den unwilligen!</code></td>
</tr>
</tbody>
</table>
</figure>
<p>What does this say?</p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 5: Use lower() and Dictionary Comprehension</h2>
<p class="has-global-color-8-background-color has-background">This method uses Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-lower/" data-type="URL" data-id="https://blog.finxter.com/python-string-lower/" target="_blank"><code>lower()</code></a> to convert <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank">Dictionary</a> values into lower case using <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary-comprehension/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary-comprehension/" target="_blank">Dictionary Comprehension</a>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">employees = {'Sandy': 'Coder', 'Kevin': 'Network Specialist', 'Amy': 'Designer'}
result = {k: v.lower() for k, v in employees.items()}
print(result)</pre>
<p>Above creates a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank">Dictionary</a> containing Rivers Employees Names and associated Job Title. This saves to <code>employees</code>.</p>
<p>Next, <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary-comprehension/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary-comprehension/" target="_blank">Dictionary Comprehension</a> is used to loop through the values in the <em>key:value</em> <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank">Dictionary</a> pairs and convert the values to lower case. The output saves to <code>result</code> and is output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code> {'Sandy': 'coder', 'Kevin': 'network specialist', 'Amy': 'designer'}</code></td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-ignore-case-in-strings/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FTlEC5Jx72Uc%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Bonus: CSV Column to title() case</h2>
<p class="has-global-color-8-background-color has-background">In this Bonus section, a <a href="https://blog.finxter.com/read-a-csv-file-to-a-pandas-dataframe/" data-type="post" data-id="440655" target="_blank" rel="noreferrer noopener">CSV file</a> you are working with contains a column of Full Names. You want to ensure all data entered in this column is in <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-title/" data-type="URL" data-id="https://blog.finxter.com/python-string-title/" target="_blank"><code>title()</code></a> case. This can be accomplished by running the following code.</p>
<p>Contents of <code>finxter_users.csv</code> file</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>FID</td>
<td> Full_Name</td>
</tr>
<tr>
<td>30022145</td>
<td>sally jenkins</td>
</tr>
<tr>
<td>30022192</td>
<td>joyce hamilton</td>
</tr>
<tr>
<td>30022331</td>
<td>allan thompson</td>
</tr>
<tr>
<td>30022345</td>
<td>harry stiller</td>
</tr>
<tr>
<td>30022157</td>
<td>ben hawkins</td>
</tr>
</tbody>
</table>
</figure>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd users = pd.read_csv('finxter_users.csv')
users['Full_Name'] = users['Full_Name'].str.title()
print(users)</pre>
<p>Above imports the <a href="https://blog.finxter.com/category/pandas-library/" data-type="URL" data-id="https://blog.finxter.com/category/pandas-library/" target="_blank" rel="noreferrer noopener">Pandas</a> library. For installation instructions, click <a href="https://blog.finxter.com/how-to-install-pandas-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-install-pandas-in-python/" target="_blank" rel="noreferrer noopener">here</a>.</p>
<p>Next, the <code>finxter_users.csv</code> file (contents shown above) is read and saved to the DataFrame <code>users</code>.</p>
<p>The highlighted line directly accesses all values in the <code>users['Full_Name']</code> column and converts each entry to <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-title/" data-type="URL" data-id="https://blog.finxter.com/python-string-title/" target="_blank"><code>title()</code></a> case. This saves back to <code>users['Full_Name']</code> and is output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td></td>
<td>FID</td>
<td> Full_Name</td>
</tr>
<tr>
<td>0</td>
<td>30022145</td>
<td>Sally Jenkins</td>
</tr>
<tr>
<td>1</td>
<td>30022192</td>
<td>Joyce Hamilton</td>
</tr>
<tr>
<td>2</td>
<td>30022331</td>
<td>Allan Thompson</td>
</tr>
<tr>
<td>3</td>
<td>30022345</td>
<td>Harry Stiller</td>
</tr>
<tr>
<td>4</td>
<td>30022157</td>
<td>Ben Hawkins</td>
</tr>
</tbody>
</table>
</figure>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Note</strong>: Don’t forget to save the DataFrame to keep the changes.</p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-ignore-case-in-strings/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F-JKVy_HliQE%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio" id="block-323ff1c3-82fc-4193-9c8d-125f5d3c2efb">Summary</h2>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio" id="block-ebf714b3-b2f5-494e-bcd5-0cd3b3709ed7">These six (6) methods of ignoring cases in Strings should give you enough information to select the best one for your coding requirements.</p>
<h2>Programmer Humor</h2>
<p class="has-global-color-8-background-color has-background"><em><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2753.png" alt="❓" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: Why do programmers always mix up Halloween and Christmas?<br /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2757.png" alt="❗" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Answer</strong>: Because Oct 31 equals Dec 25.</em></p>
<p><em>(If you didn’t get this, read our articles on the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-oct-function/" data-type="post" data-id="24365" target="_blank"><code>oct()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-int-function/" data-type="URL" data-id="https://blog.finxter.com/python-int-function/" target="_blank"><code>int()</code></a> Python built-in functions!)</em></p>
</p></p>
</div>
https://www.sickgaming.net/blog/2022/08/...n-strings/