Python strings.docx - Python strings last modified July 6...

This preview shows page 1 - 5 out of 23 pages.

Python strings last modified July 6, 2020 In this part of the Python programming tutorial, we work with string data in more detail. Python string definition A string in Python is a sequence of characters. It is a derived data type. Strings are immutable. This means that once defined, they cannot be changed. Many Python methods, such as replace() , join() , or split() modify strings. However, they do not modify the original string. They create a copy of a string which they modify and return to the caller. Python string literals Python strings can be created with single quotes, double quotes, or triple quotes. When we use triple quotes, strings can span several lines without using the escape character. string_literals.py #!/usr/bin/env python # string_literals.py a = "proximity alert" b = 'evacuation' c = """ requiem for a tower """ print(a) print(b) print(c) In our example we assign three string literals to a , b , and c variables. And we print them to the console. $ ./string_literals.py proximity alert
evacuation requiem for a tower Unicode in Python If we want to create Unicode strings, we add a u or U character at the beginning of the text. unicode.py #!/usr/bin/env python # unicode.py text = u'\u041b\u0435\u0432 \u041d\u0438\u043a\u043e\u043b\u0430\ \u0435\u0432\u0438\u0447 \u0422\u043e\u043b\u0441\u0442\u043e\u0439: \n\ \u0410\u043d\u043d\u0430 \u041a\u0430\u0440\u0435\u043d\u0438\u043d\u0430' print(text) In our example, we print Leo Tolstoy: Anna Karenina in azbuka. $ ./unicode.py Лев Николаевич Толстой: Анна Каренина We can use the Russian letters directly if we use the encoding comment. unicode2.py #!/usr/bin/python # -*- coding: utf-8 -*- # unicode2.py text = 'Лев Николаевич Толстой: Анна Каренина' print(text) In this example, we use non-latin characters directly in the source code. We have defined UTF-8 encoding with a encoding comment. Using quotes in Python
Strings in Python are delimited by single or double quote characters. What if we wanted to display quotes, for example in a direct speech? There are two basic ways to do this. quotes.py #!/usr/bin/env python # quotes.py print("There are many stars.") print("He said, \"Which one is your favourite?\"") print('There are many stars.') print('He said, "Which one is your favourite?"') We use the \ character to escape additional quotes. Normally the double quote character is used to delimit a string literal. However, when escaped, the original meaning is suppressed. It appears as a normal character and can be used within a string literal. The second way to use quotes within quotes is to mix single and double quotes. $ ./quotes.py There are many stars. He said, "Which one is your favourite?" There are many stars. He said, "Which one is your favourite?" This is the output. Python string length The len() method calculates the number of characters in a string. The white characters are also counted. string_length.py #!/usr/bin/env python # string_length.py s1 = "Eagle" s2 = "Eagle\n" s3 = "Eagle " print(len(s1)) print(len(s2)) print(len(s3))
We compute the length of three strings.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture