A cikin duniyar fashion, yanayi da salo suna canzawa koyaushe. Koyaya, ɗayan ɓangaren da ya saura a tsaye shine amfani da tsarin rubutu. Ko a cikin ƙira, yin alama, ko abun cikin kan layi, gabatar da rubutu a lokuta daban-daban yana da mahimmanci kuma yana iya ƙirƙirar saƙo mai ƙarfi. A cikin wannan labarin, za mu tattauna aikin gama gari na shirye-shiryen da ke da alaƙa da wannan: canza ƙarami zuwa babban harka ta amfani da Python. Don cimma wannan, za mu yi amfani da ginanniyar ayyuka da ɗakunan karatu na Python, tare da cikakken bayani mataki-mataki na lambar.
Ayyukan Wurin Gina na Python: babba()
Hanya ta farko kuma mafi sauƙi don canza ƙaramin kirtani zuwa babban kirtani a Python shine ta amfani da ginanniyar ciki. hanyar kirtani kira na sama (). Wannan hanyar tana ba ku damar canza duk ƙananan haruffa a cikin wani kirtani da aka bayar zuwa takwarorinsu na manya.
original_text = "converting lower case to upper case" upper_case_text = original_text.upper() print(upper_case_text)
A cikin misalin da ke sama, mun fara ayyana ma'anar kirtani da ake kira asali_rubutu mai dauke da karamar jumla. Sa'an nan, mu yi amfani da na sama () hanya a kan original_text don canza shi zuwa babban kirtani, sanya sakamakon zuwa sabon canji mai suna babban_rubutu. A ƙarshe, muna buga rubutun babban harafi da aka canza.
- original_text = "canza karamar harka zuwa babba"
- babba_case_text = original_text.upper()
- buga(rubutu_babba)
Amfani da Ayyukan Musamman don Canjin Harka
Yayin gina-in na sama () Aiki yana da sauƙin amfani, ƙila ku haɗu da yanayi inda ake buƙatar ingantaccen bayani. A irin waɗannan lokuta, zaka iya ƙirƙirar naka aikin al'ada don canza ƙaramin harafi zuwa babban harka.
def convert_to_upper_case(rubutu):
babba_text = ""
don hali a cikin rubutu:
ascii_code = ord (hali)
idan 97 <= ascii_code <= 122: # Bincika idan harafin karamar harafi ce babba_case_text += chr(ascii_code - 32) sauran: babba_case_text += harafin mayar da babban_case_text original_text = "mayarda karamin harka zuwa babban harka" babba_case_text = convert_to_upper_text(original) ) print(upper_case_text) [/code] A cikin wannan al'ada aikin da ake kira maida_zuwa_babban_case, muna maimaita ta kowane hali na kirtani shigarwa rubutu kuma duba idan ƙaramin harafi ne ta kwatanta lambar ta ASCII. Idan ƙaramin harafi ne, za mu cire 32 daga lambar ASCII don samun lambar babban harafin da ya dace, wanda sai mu haɗa zuwa ga babban_rubutu m. Idan harafin ba ƙaramin harafi ba ne, muna kawai saka shi zuwa babban_text ba tare da yin wani canji ba.
Yin amfani da ɗakin karatu na Python: str
Wani ɗakin karatu a Python, ana kiransa kawai str, an tsara shi don gudanar da ayyukan kirtani. Yana ba da ayyuka da yawa don sarrafa ko tantance bayanan rubutu. Wannan ɗakin karatu, a gaskiya, ya riga ya samar da na sama () aikin da muka yi amfani da shi a baya a matsayin ɓangare na str aji. Amfani da waɗannan hanyoyin daga str aji na iya sa ayyukanku masu alaƙa da sarrafa rubutu su yi sauƙi da inganci.
A ƙarshe, canza ƙarami zuwa babban harka a Python ana iya cimma ta da hanyoyi daban-daban. An ginannen Python na sama () Hanyar tana ba ku damar sauya igiyoyin shigarwa da sauri zuwa takwarorinsu na manya. A madadin, zaku iya ƙirƙirar aikin al'ada wanda ke ba ku ƙarin iko akan tsarin juyawa. A ƙarshe, Python's str ɗakin karatu ya ƙunshi ayyuka masu amfani da yawa, gami da na sama () hanyar, wanda ke sa sarrafawa da sarrafa bayanan kirtani mafi inganci da dacewa.