Nauč se Python > Materiály > Snake Workshop for PyLadies > Introduction to Python > Strings

This is a machine-generated translation. If you're not at the in-person workshop, try the DjangoGirls tutorial for an intro to Python!

Strings #

Numbers are very useful for computers (as the word computer suggests), but Python can also work with other types of information. For example, with text.

Try it: enter your name in quotation marks, as you can see below:

>>> 'Ola'
'Ola'

You have now created your first string! A string is a programming term for text - a sequence of characters (letters) that can be processed by a computer.

When entering a string, you must always enclose it in quotation marks (apostrophes). Otherwise, Python would not recognize what is text to work with and what are instructions to execute. This is quite important for a computer - people will understand such things from the context but a computer is just a machine.

(Ilustrační komiks. Člověk říká robotovi: "Řekni Pavlovi, ať mi zavolá!". Robot odpoví: "PAVLOVI AŤ MI ZAVOLÁ!")

Strings can be concatenated - "added" - using the + operator. Try this:

>>> 'I am ' + 'Ola'
'I am Ola'

Watch out for the space! When you enter 'I am' + 'Ola', the two words will be joined together. The computer considers the space as a character; it behaves towards it like towards any letter. If you don't put the space in quotation marks, it won't be part of the string.

Spaces between the string and the operator (+) do not matter. All of the following commands do the same thing:

>>> 'I am ' + 'Ola'
'I am Ola'
>>> 'I am '+'Ola'
'I am Ola'
>>> 'I am '                +     'Ola'
'I am Ola'

But it is customary to write one space around the operator on each side - just like in these materials. The code is then more readable.

Try putting the space itself in quotation marks - add up three strings:

>>> 'I am' + ' ' + 'Ola'
'I am Ola'

In addition to "concatenation", you can also repeat strings - multiply by a number.

>>> 'Ola' * 3
'OlaOlaOla'

Quoting #

And what if you want to put an apostrophe inside your string? You can use double quotes around the string.

>>> "I'd say they're properly crazy!"
"I'd say they're properly crazy!"

Python doesn't care which type of quotation marks you use to enter a string. The important thing is the letters inside. When Python prints a string, it can choose a different type of quotation marks than the ones you used.

>>> "Ola"
'Ola'

Functions and methods #

You already know how to 'add' strings ('Hello ' + 'Ola!') and 'multiply' them ('Ola' 3). For all other things that can be done with text, there are not enough symbols on the keyboard. Therefore, some operations are named verbally - for example, so-called functions*.

If you want to know the number of letters in your name, call the function 'len'. Write 'len' (without quotes), then parentheses, and inside those parentheses, your name as a string (in quotes). This way you will call the function 'len' on the string with your name.

>>> len('Ola')
3

There is a function called type which determines whether something is a number or a string. How would you call it on the number 123 or the string 'abc'?

Řešení

In addition to functions, there are methods, which are written a little differently.

If you want to see your name in capital letters, call the upper method. Write a string, then a dot, the method name upper (without quotes), and empty parentheses.

>>> 'Ola'.upper()
'OLA'

Strings also have a method called lower. Try calling it on your name.

Řešení

What is a method (which you call with a dot, like 'Ola'.upper()) and what is a function (where you insert information in parentheses like len('Ola')), you will have to remember or look up for each new function/method.

Expression evaluation #

You can use calling a function or method as another value.

Let Python calculate the mathematical expression (1 + 3) / 2:

>>> 8 / (1 + 3)
2.0

Python first adds 1 + 3 and the result is 4. It replaces the sum with 4 in the original equation and gets 8 / 4. Then it divides and gets 2.0.

Try to think about how Python will process these expressions:

>>> len('Ola') + 1
4
>>> 'I am ' + 'Ola'.upper()
'I am OLA'
`
>>> len('Ola' * 3)
9
>>> len('Ola'.upper())
3

Řešení

Similar composition is very common in programming. Most of the basic building blocks can be learned by beginners in a few weeks or months - and then throughout their programming career they discover new ways to assemble them into more and more complex structures.

Summary #

OK, enough of the strings. What have you learned so far?

  • Strings are used for working with text.
  • Operators + and * are used for concatenating and repeating strings.
  • Functions and methods like len() and upper() perform some actions on strings.
  • Expressions can be composed together.

Numbers, strings, operators, and functions are the basics of most programming languages.

Ready for something new? We bet you are!


Toto je stránka lekce z kurzu, který probíhá nebo proběhl naživo s instruktorem.