Lesson-19: Python Character Sequences-2

Python Character Sequences-2

Python Character Sequences, In our previous lesson, we talked about the general characteristics of character sequences. In this lesson, we will talk about some methods. Methods are tools in Python that allow us to change, query, or add new properties to objects. Thanks to these methods, we will address the new characteristics of character sequences.

replace()

As the name suggests, with this method, we can replace the characters in the character strings with other characters that we will specify.

ch="btogrenme"
chNew=ch.replace("b","B")

print (chNew)

EKRAN ÇIKTISI:
Btogrenme

If you examine the codes above. with the replace method, we made the “b” in the CH Variable “B”. we transferred it to a new variable called chNew and printed it on the screen.

ch="btogrenme"
chNew=ch.replace("bt","BT")

print (chNew)

EKRAN ÇIKTISI:
BTogrenme

If the part we are going to change is a limited number, we need a third parameter. You can examine the following example.

ch="Python Lessons"
chNew=ch.replace("s","S",2)

print (chNew)

EKRAN ÇIKTISI:
Python LeSSons

The example above replaces the letters s with S. But with the third parameter, this change is limited. It changes only 2 characters.

split()

Segments that divide character sequences from specific points.

ch="Red Blue Purple"
chNew=ch.split()

print (chNew)

EKRAN ÇIKTISI
['Red', 'Blue', 'Purple']

As a result, he produced a list. If we want, we can create a loop in this list with the for loop and print the values on the screen one by one. When splitting, the split method performs the splitting process based on the space character. In other words, division is made from gaps. If it is used with the parameter, this is not the case.

ch="Red * Blue * Purple"
chNew=ch.split("*")

print (chNew)

EKRAN ÇIKTISI
['Red', 'Blue', 'Purple']

In the example above, we used the split method with a parameter. Thus, the fragmentation process took place based on the character * in the variable ch.

lower ():

In some programs, the data requested from the user is case sensitive. For example, if a user is asked for a password, the user must type this password with caution. In terms of these programs, for example, ‘password’ and ‘password’ are not the same words. For example, if the user’s password is ‘password’, this user cannot enter the program by typing ‘password’.

In some other programs, the opposite is true. In other words, unlike case-sensitive programs, some programs do not care whether the data from the user is uppercase or lowercase. If the user type the correct word in uppercase or lowercase, the program performs the desired operation. For example, Google searches work on this logic. For example, if you search for the word’ book’ on Google, Google will show you the same results if you type this word in uppercase or lowercase. From Google’s point of view, it doesn’t matter if you type the word you’re looking for in uppercase or lowercase.

Let’s examine the following example:

kişi = input("Aradığınız kişinin adı ve soyadı: ")
kişi = kişi.lower()

if kişi == "ahmet öz":
    print("email: [email protected]")
    print("tel  : 02121231212")
    print("şehir: istanbul")

elif kişi == "mehmet söz":
    print("email: [email protected]")
    print("tel  : 03121231212")
    print("şehir: ankara")

elif kişi == "mahmut göz":
    print("email: [email protected]")
    print("tel  : 02161231212")
    print("şehir: istanbul")

else:
    print("Aradığınız kişi veritabanında yok!")

In the example above, the user is asked for a name. kişi = kişi.lower(), this variable is completely converted to lower case. This means that even if the user enters “AHMET ÖZ”, “Ahmet Öz”, “AHmet Öz”, all characters will be converted to lowercase, so the corresponding part will be active in the If block.

>>> kardiz = "ELMA"
>>> kardiz.lower()

'elma'

>>> kardiz = "arMuT"
>>> kardiz.lower()

'armut'

>>> kardiz = "PYTHON PROGRAMLAMA"
>>> kardiz.lower()

'python programlama'

upper ():

This method performs the opposite of the lower () method. Capitalizes all elements of the character.

şehir = input("Hava durumunu öğrenmek için bir şehir adı girin: ")
şehir = şehir.upper()

if şehir == "ADANA":
    print("parçalı bulutlu")

elif şehir == "ERZURUM":
    print("karla karışık yağmurlu")

elif şehir == "ANTAKYA":
    print("açık ve güneşli")

else:
    print("Girdiğiniz şehir veritabanında yok!")

In the above example, the user is asked to enter a province. If this value is entered, it is completely converted to capital letters using the upper () method.

islower(), isupper()

These methods, on the other hand, query the state of the character array without making any changes to the character array. Let’s look at the following examples.

>>> ch= "istihza"
>>> ch.islower()

True

>>> ch= "Ankara"
>>> ch.islower()

False
veri = input("Adınız: ")

if not veri.islower():
    print("Lütfen isminizi sadece küçük harflerle yazın")

In the example above, a warning message is printed on the screen if the data entered by the user is not completely lowercase. The user is expected to log in completely in lowercase letters.

endswith()

With this method, we can question which sequence of characters ends with a sequence of characters. So, for example,:

>>> ch= "istihza"
>>> ch.endswith("a")

True

Consider the following example carefully.

veri1 = "python.doc"
veri2 = "tkinter.mp3"
veri3 = "pygtk.ogg"
veri4 = "movie.avi"
veri5 = "sarki.mp3"
veri6 = "filanca.ogg"
veri7 = "falanca.mp3"
veri8 = "dosya.avi"


for i in veri1, veri2, veri3, veri4, veri5, veri6, veri7, veri8:
    if i.endswith(".mp3"):
        print(i)

In the example above, a loop is opened from veri1 to veri8, and the last characters in this data are printed on the screen with “.mp3”.

startswith():

This method does the opposite of what the endswith() method we just saw does. If you remember, the endswith() method controlled which characters or characters an array of characters ends with. the startswith () method controls which characters or characters start an array of characters.

>>> ch= "python"
>>> ch.startswith("p")

True

>>> ch.startswith("a")

False

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *