Formatarea sirurilor de caractere si operatii cu siruri de caractere

postat acum 3 ani de Stefanescu Mihai in categorie Python

Sirurile de caractere este unul dintre cele mai populare tipuri de date din python. Putem crea un sir de caractere (string) pur si simplu punandu-l intre ghilimele sau apostroafe (in python “ si ‘ sunt tratate la fel).

variabila1 = 'Salut!'
varariabila2 = "Salut Invata-Programare!"

Accesarea valorilor din siruri de caractere

Pentru a accesa substring-uri (portiuni din siruri de caractere) va trebui sa sa folosim parantezele patrate pentru sliceing:

variabila1 = 'Salut!'
varariabila2 = "Salut Invata-Programare!"
print "variabila1[0]: ", variabila1[0]
print "variabila2[1:5]: ", variabila2[1:5]

Codul de mai sus va avea urmatorul rezultat:

variabila1[0]: S
variabila2[1:5]: alut

Update-ul unui string

Poti face “update” unui sir de caractere re-asignandu-i alta valoare. Aceasta noua valoare poate fi scris in continuarea celei vechi sau o poate inlocui pe cea veche.

variabila1 = 'Salut'
print "Noul sir de caractere: ", variabila1 + ''

Acest cod va avea rezultatul:

Noul sir de caractere: Salut

Operatori de formatare a stringurilor

Python ne permite sa formatam sirurile de caractere folosindu-ne de operatorul %. Acest operator se aplica numai in cazul sirurilor de caractere si este similar cu functia printf din C (si din limbajele ce fac parte din aceasta familie).

print "Salut! Numele meu este %s si am %d ani!" % ('Stefanescu Mihai', 23) 

Iata o lista cu toate symbolurile ce pot fi folosite ina cest mod:




Un caracter


Un sir de caractere


Numar intreg


Numar zecimal


Numar intreg mai mare de 0


Numar intreg octal


Numar intreg hexazecimal


Numar intreg hexazecimal


Notare exponentiala


Notare exponentiala




Similar cu %f si %e


Similar cu %f si %E

Ghilimele triple

Ghilimelele triple in Python ne ajuta la scrierea unui sire de caractere ce se intinde pe mai multe randuri.


string_mare = """this is a long string that is made up of
several lines and non-printable characters such as
TAB ( \t ) and they will show up that way when displayed.
NEWLINEs within the string, whether explicitly given like
this within the brackets [ \n ], or just a NEWLINE within
the variable assignment will also show up.
print string_mare

Siruri de carcatere unicode

In mod normal sirurile de carctere din Python sunt stocate ca si ASCII 8bits, in timp ce sirurile de caractere unicode sunt stocate in 16-bit Unicode. Acest unicode ne permite sa stocam o gama mai larga de caractere, inclusiv caractere speciale (diacritice).

print u'Salut, comunitatea!'


Sunt un tanar programator din Bucuresti ce lucreaza in PHP/Mysql (MySqli/PDO), Laravel, CodeIgniter, MySQL, PostgreSQL, Wordpress, HTML5/CSS3, Sass, Photoshop si multe altele.
Google+ Community Facebook Group

Comentariu postate de minecraft download free la data de 14.05.2019
I've been surfing on-line greater than three hours today, yet I by no means discovered any interesting article like yours. It's pretty worth enough for me. In my view, if all webmasters and bloggers made good content as you did, the net will likely be much more useful than ever before.
Comentariu postate de download minecraft for free la data de 14.05.2019
Thank you for the good writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how could we communicate?
Comentariu postate de how to download minecraft la data de 16.05.2019
It's an remarkable article in favor of all the online visitors; they will take benefit from it I am sure.
Comentariu postate de download minecraft free la data de 17.05.2019
Have you ever thought about including a little bit more than just your articles? I mean, what you say is fundamental and all. But think of if you added some great photos or videos to give your posts more, "pop"! Your content is excellent but with pics and clips, this site could certainly be one of the greatest in its field. Very good blog!