Cara menggunakan ignore letter case python

Gaya penulisan Snake Case biasanya kita jumpai pada pemrograman prosedural dan fungsional seperti C, Pascal, PHP, dsb.

2. Gaya Camel Case

Gaya penulisan Camel Case biasanya sering digunakan pada pemrograman berorientasikan objek atau OOP.

Cirinya, semua suku kata menyatu dan terdapat huruf kapital untuk memisahnya.

Hal tersebut membuatnya terlihat seperti punggung onta. Karena itu, gaya ini disebut Camel Case.

Contohnya seperti ini:

TheQuickBrownFoxJumpsOverTheLazyDog iniPenulisanCamelCase(); NamaClass namaVariabel iPhone eBay camelCase

3. Kebab Case

Mendengar kata Kebab jadi lapar… 😄

Gaya Kebab Case menggunakan tanda minus (-) untuk memisah suku kata. Sehingga membuatnya terlihat seperti kebab.

Karena itu, gaya ini dinamakan Kebab Case.

Contohnya seperti ini:

ini-gaya-kebab-case The-quick-brown-fox-jumps-over-the-lazy-dog btn-primary -moz-transition

Gaya penulisan Kebab Case biasanya kita jumpai pada CSS.

4. All caps

All Caps artinya semua kapital.

Gaya penulisan ini sering digunakan untuk menuliskan nama sebuah konstanta.

Contohnya:

PI DATABASE HOSTNAME URL_STRING

Manakah Gaya yang kamu sukai?

Saya sendiri menyukai gaya Camel Case, karena cukup mudah mengetiknya. Tapi kadang juga menggunakan Snake Case.

A programming language is said to be case sensitive if it differentiates between the uppercase and lowercase characters. In this article, we will learn why Python is a case sensitive language and how we can ignore it in Python.

Scope

  • In this article we will learn what case sensitivity means and if Python is a case sensitive language or not.
  • We will learn some variable naming standards in Python and when we should use upper or lower case.
  • We will also learn how to ignore case in a given Python program.
  • Finally, we will look into why Python is considered a case sensitive language.

Introduction to Case Sensitivity in Python

While logging into a website, have you ever tried mixing the uppercase and lowercase letters in your password? For example, typing myP@SSword instead of MyP@ssWord. You would have noticed that the uppercase and lowercase letters are not treated as the same and you are not allowed to login if you change the case.

This is an application of case sensitivity. A case sensitive programming language means that the program will treat the uppercase and lowercase letters differently. Hence, we must use the exact case according to the syntax because changing the case, for example from print to Print, will result in an error.

In the subsequent sections, we will learn whether Python is a case-sensitive language with some examples.

Is Python a Case-Sensitive Language?

YES, Python is a case-sensitive programming language. This means that it treats uppercase and lowercase letters differently. Hence, we cannot use two terms having same characters but different cases interchangeably in Python.

Suppose we write a function calculateArea() to accept a circle's radius and print its area. Let us see how case difference results in error in one case but gives the expected output in the other.

Example 1 (wrong case):

Code:

def calculateArea(radius): PI = 3.1416 Print(PI * radius * radius) circle_radius = 5 calculateArea(circle_radius)

Output:

Traceback (most recent call last): File "main.py", line 6, in calculateArea(circle_radius) File "main.py", line 3, in calculateArea Print(PI * radius * radius) NameError: name 'Print' is not defined

NameError: name 'Print' is not defined

Example 2 (right case):

Code:

def calculateArea(radius): PI = 3.1416 print(PI * radius * radius) circle_radius = 5 calculateArea(circle_radius)

Output:

Explanation:

Did you notice that only a case difference in print resulted in two different outputs? According to Python's syntax, the keyword print should always be written in lowercase. Hence when we changed its case in example 1, Python could not identify it, resulting in NameError. When we corrected the case in example 2, we got the correct output as expected.

Why is Python Case Sensitive?

Python is called a case sensitive language because the uppercase and lowercase characters are distinguished during execution. Two terms in Python are treated differently if their case is different, even though the characters might be the same. This results in an error if we try to access a value with a different case.

The main reason why Python is structured this way is that Python finds its applications in various fields. We would not want to restrict the number of identifiers and symbols which can be used, hence case sensitivity is allowed. In fact, most of the popular high level programming languages like Java, C, C++ and JavaScript are case sensitive.

Variable Naming Standards in Python: When to Use Upper or Lower Case?

There are some variable naming standards that we should follow while writing a Python program. These are not entirely mandatory but they make our code cleaner and more readable.

  • Variable and Function names should be in lowercase, with words separated by underscores to improve readability. Ex: circle_radius = 5
  • Packages and Modules should also be written in lowercase. Ex: import math
  • Class names should have the first letter of every word in uppercase. They should not be separated by an underscore. Ex: class AreaCalculator
  • Constants should entirely be in uppercase and should use underscore to separate words. Ex: PI = 3.1416

Note:

While the above naming standards are highly recommended and good coding practices, remember that it will not lead to any errors if we do not follow them strictly.

In most scenarios, the username for logging into a website is not case sensitive. Suppose my username is python-user, I should be able to login even if I type something like Python-User or PYTHON-USER. How can we make Python ignore the case while checking for equality? We can make use of the .upper() or .lower() methods in Python to change the case of a string.

  • .upper(): It converts all the characters in a given string to uppercase.
  • .lower(): It converts all the characters in a given string to lowercase.

Suppose we need to design a login page where the password is case sensitive but the username ignores the case. We will take both inputs from the user, convert the username to uppercase (or lowercase) and check for its equality with the expected username, which is also converted to uppercase (or lowercase). The password is case sensitive, hence we do not need to convert it to upper or lowercase.

For the username, Python will only check if the string matches by character, ignoring the cases of the input and the expected string. On the contrary, the password check will include both the character and the case matching. Let's see this in action:

Code:

inputUsername = "Python-User" username = "python-user" inputPassword = "myP@SSword" password = "myP@SSword" print("Scenario 1: Case Ignored") if (inputUsername.lower() == username.lower() and inputPassword == password): print("Successful login") else: print("Incorrect username or password") print() print("Scenario 2: Case Not Ignored") if (inputUsername == username and inputPassword == password): print("Successful login") else: print("Incorrect username or password")

Output:

Scenario 1: Case Ignored Successful login Scenario 2: Case Not Ignored Incorrect username or password

Explanation:

In Scenario 1, the case of the username is ignored using .lower() method. Hence, login is successful even though the case of the username inputted by the user and that in the records is different. In Scenario 2, we do not use .lower() or .upper() methods. Hence the case is not ignored and the equality check considers the cases of both usernames. The login is unsuccessful since the case of both usernames are different.

Note:

In the above example, we have simplified the login scenario by assuming that there is only one correct username and password combination. We have not used .lower() or .upper() to ignore the cases of the password since passwords are always case sensitive.

Postingan terbaru

LIHAT SEMUA