Python Dizesi startswith ()

Startswith () yöntemi, bir dize belirtilen önek (dize) ile başlıyorsa True döndürür. Değilse, False döndürür.

Sözdizimi startswith()şöyledir:

 str.startswith (önek (, başlangıç ​​(, bitiş)))

startswith () Parametreler

startswith() yöntem en fazla üç parametre alır:

  • önek - Kontrol edilecek dizeler veya dizeler dizisi
  • start (isteğe bağlı) - Dize içinde ön ekin kontrol edileceği başlangıç ​​konumu .
  • (opsiyonel) - pozisyonunu Bitiş önek dize içinde kontrol edilmelidir.

Startswith () 'den Dönüş Değeri

startswith() yöntem bir boole döndürür.

  • Dize belirtilen önek ile başlıyorsa True döndürür.
  • Dize belirtilen önekle başlamazsa False döndürür.

Örnek 1: startswith () Başlangıç ​​ve bitiş parametreleri olmadan

 text = "Python is easy to learn." result = text.startswith('is easy') # returns False print(result) result = text.startswith('Python is ') # returns True print(result) result = text.startswith('Python is easy to learn.') # returns True print(result)

Çıktı

 Yanlış Doğru Doğru

Örnek 2: startswith () Başlangıç ​​ve bitiş Parametreleri ile

 text = "Python programming is easy." # start parameter: 7 # 'programming is easy.' string is searched result = text.startswith('programming is', 7) print(result) # start: 7, end: 18 # 'programming' string is searched result = text.startswith('programming is', 7, 18) print(result) result = text.startswith('program', 7, 18) print(result)

Çıktı

 Doğru Yanlış Doğru

Tuple'ı startswith () 'e geçme

startswith()Python'da metoda bir demet önek geçirmek mümkündür .

Dize, başlığın herhangi bir öğesiyle başlıyorsa startswith()True döndürür. Değilse, False döndürür

Örnek 3: startswith () Tuple Öneki ile

 text = "programming is easy" result = text.startswith(('python', 'programming')) # prints True print(result) result = text.startswith(('is', 'easy', 'java')) # prints False print(result) # With start and end parameter # 'is easy' string is checked result = text.startswith(('programming', 'easy'), 12, 19) # prints False print(result)

Çıktı

 Doğru Yanlış Yanlış

Bir dizenin belirtilen sonekle bitip bitmediğini kontrol etmeniz gerekiyorsa, Python'da endswith () yöntemini kullanabilirsiniz.

Ilginç makaleler...