DEV Community

Cover image for String in Python
islomAli99
islomAli99

Posted on • Updated on

String in Python

STRING (matn) —Pythondagi eng mashxur ma'lumot turlaridan biri. Avvalgi darsda ko'rganimizdek, o'zgaruvchiga matn yuklash uchun matn qo'shtirnoq (" ") yoki birtirnoq (' ') ichida yozilishi kerak.

ism = "Abdumajid"
familiya = 'Akhmadjanov'

Enter fullscreen mode Exit fullscreen mode

STRING USTIDA AMALLAR
Matnlarni qo'shish operatori (+)
Matnlarni qo'shish uchun + operatoridan foydalanmiz:

ism = 'Diyorbek'
print("Mening ismim " + ism)
Enter fullscreen mode Exit fullscreen mode

Natija: Mening ismim Diyorbek

ism = 'Islam'
familiya = 'Akhmadjanov'
print(ism + familiya)
Enter fullscreen mode Exit fullscreen mode

Natija: Islam Akhmadjanov

f-string
Ikki (va undan ko'p) matn ko'rinishidagi o'zgaruvchilarni birlashtirish uchun f-string usulidan f"{matn1} {matn2}" ham foydalansak bo'ladi:

ism = "Javohir"
familiya = 'Ravshanov'
ism_sharif = f"{ism} {familiya}"
print(ism_sharif)
Enter fullscreen mode Exit fullscreen mode

Natija: Javohir Ravshanov

Bu usul yordamida uzun matnlarni ham yasash mumkin:

ism = "James"
familiya = 'Bond'
print(f"Salom, mening ismim {familiya}. {ism} {familiya}!")
Enter fullscreen mode Exit fullscreen mode

Natija: Salom, mening ismim Bond. James Bond!

Mahsus belgilar
Matnga bo'shliq qo'shish uchun \t belgisidan, yangi qatordan boshlash uchun \n belgisidan foydalanamiz:

print('Hello World!')
print('Hello \tWorld!')
print('Hello \nWorld!')
Enter fullscreen mode Exit fullscreen mode

Natija:
Hello World!
Hello World!
Hello
World!

Top comments (0)