In this article, we will learn what is strings in a programming language, how to create them, and their uses. Further, we will study various operators to compare strings in python. At last, we will study some Python strings comparison in brief along with its python code example and output. So, let’s get started! Show
What are Strings?A string is generally a sequence of characters. A character is a simple symbol. For example, in the English Language, we have 26 characters available. The computer system does not understand characters and hence, therefore, deal with binary numbers. Even though we can see characters on our monitor screens, but internally it is stored and manipulated as a combination of 0s and 1s. The conversion of characters and the binary number is called encoding, and the reverse of this is known as decoding. Some of the popular encodings are ASCII and Unicode. In the Python programming language, a string is a sequence of Unicode characters. Python String Comparison operatorsIn python language, we can compare two strings such as identify whether the two strings are equivalent to each other or not, or even which string is greater or smaller than each other. Let us check some of the string comparison operator used for this purpose below:
String Equals Check in PythonIn python programming we can check whether strings are equal or not using the “==” or by using the “.__eq__” function. Example: s1 = 'String' s2 = 'String' s3 = 'string' # case sensitive equals check if s1 == s2: print('s1 and s2 are equal.') if s1.__eq__(s2): print('s1 and s2 are equal.') Here, we check string s1 and s2 whether they are equal or not, and then use the “if” conditional statement with a combination of the equal operator. The output of the above code is as given below: s1 and s2 are equal. s1 and s2 are equal. What about Case insensitive comparisons?While checking the equality in strings sometimes we wish to ignore the case of the string while comparison. So, as a solution to this, we can use the case fold(), lower(), or upper() function for ignoring the case insensitive comparison of string equality. s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison') The output of the above code is as given below:
string string s1 and s3 are equal in case-insensitive comparison string string s1 and s3 are equal in case-insensitive comparison STRING STRING s1 and s3 are equal in case-insensitive comparison ConclusionSo, in this article, we studied how to compare strings in a python programming language. Also, we studied some string comparison operators and check string equality. Even we checked the string case insensitive comparison. Dalam bahasa pemrograman Python, struktur data yang paling dasar adalah urutan atau lists. Setiap elemen-elemen berurutan akan diberi nomor posisi atau indeksnya. Indeks pertama dalam list adalah nol, indeks kedua adalah satu dan seterusnya. Python memiliki enam jenis urutan built-in, namun yang paling umum adalah list dan tuple. Ada beberapa hal yang dapat Anda lakukan dengan semua jenis list. Operasi ini meliputi pengindeksan, pengiris, penambahan, perbanyak, dan pengecekan keanggotaan. Selain itu, Python memiliki fungsi built-in untuk menemukan panjang list dan untuk menemukan elemen terbesar dan terkecilnya. Membuat List PythonList adalah tipe data yang paling serbaguna yang tersedia dalam bahasa Python, yang dapat ditulis sebagai daftar nilai yang dipisahkan koma (item) antara tanda kurung siku. Hal penting tentang daftar adalah item dalam list tidak boleh sama jenisnya. Membuat list sangat sederhana, tinggal memasukkan berbagai nilai yang dipisahkan koma di antara tanda kurung siku. Dibawah ini adalah contoh sederhana pembuatan list dalam bahasa Python.
Akses Nilai Dalam List PythonUntuk mengakses nilai dalam list python, gunakan tanda kurung siku untuk mengiris beserta indeks atau indeks untuk mendapatkan nilai yang tersedia pada indeks tersebut. Berikut adalah contoh cara mengakses nilai di dalam list python :
Setelah Anda mengeksekusi kode diatas, hasilnya akan seperti dibawah ini :
Update Nilai Dalam List PythonAnda dapat memperbarui satu atau beberapa nilai di dalam list dengan memberikan potongan di sisi kiri operator penugasan, dan Anda dapat menambahkan nilai ke dalam list dengan metode append (). Sebagai contoh :
Hapus Nilai Dalam List PythonUntuk menghapus nilai di dalam list python, Anda dapat menggunakan salah satu pernyataan del jika Anda tahu persis elemen yang Anda hapus. Anda dapat menggunakan metode remove() jika Anda tidak tahu persis item mana yang akan dihapus. Sebagai contoh :
Operasi Dasar Pada List PythonList Python merespons operator + dan * seperti string; Itu artinya penggabungan dan pengulangan di sini juga berlaku, kecuali hasilnya adalah list baru, bukan sebuah String. Sebenarnya, list merespons semua operasi urutan umum yang kami gunakan pada String di bab sebelumnya. Dibawah ini adalah tabel daftar operasi dasar pada list python.
Indexing, Slicing dan Matrix Pada List PythonKarena list adalah urutan, pengindeksan dan pengiris bekerja dengan cara yang sama untuk list seperti yang mereka lakukan untuk String. Dengan asumsi input berikut :
Method dan Fungsi Build-in Pada List PythonPython menyertakan fungsi built-in sebagai berikut :
Python menyertakan methods built-in sebagai berikut
|