$$ \newcommand{\floor}[1]{\left\lfloor{#1}\right\rfloor} \newcommand{\ceil}[1]{\left\lceil{#1}\right\rceil} \renewcommand{\mod}{\,\mathrm{mod}\,} \renewcommand{\div}{\,\mathrm{div}\,} \newcommand{\metar}{\,\mathrm{m}} \newcommand{\cm}{\,\mathrm{cm}} \newcommand{\dm}{\,\mathrm{dm}} \newcommand{\litar}{\,\mathrm{l}} \newcommand{\km}{\,\mathrm{km}} \newcommand{\s}{\,\mathrm{s}} \newcommand{\h}{\,\mathrm{h}} \newcommand{\minut}{\,\mathrm{min}} \newcommand{\kmh}{\,\mathrm{\frac{km}{h}}} \newcommand{\ms}{\,\mathrm{\frac{m}{s}}} \newcommand{\mss}{\,\mathrm{\frac{m}{s^2}}} \newcommand{\mmin}{\,\mathrm{\frac{m}{min}}} \newcommand{\smin}{\,\mathrm{\frac{s}{min}}} $$

Prijavi problem


Obeleži sve kategorije koje odgovaraju problemu

Još detalja - opišite nam problem


Uspešno ste prijavili problem!
Status problema i sve dodatne informacije možete pratiti klikom na link.
Nažalost nismo trenutno u mogućnosti da obradimo vaš zahtev.
Molimo vas da pokušate kasnije.

Text values

In addition to integers and real numbers, one of the basic types of data in programming is text. The text data is called a string. In addition to letters, they can contain all other characters used in the text: punctuation, parentheses, numbers, mathematical operators, various special characters such as %, $, ^. & etc.

Text values are written between quotation marks. We call the text under quotation marks a text constant or a literal. Single '...' and double "..." quotes can equally be used in Python (it is only important that quotes are of the same type at the beginning and end of the string). For example:

s1 = 'One text'
s2 = "Another text"

We will use the word string for the textual data type, as well as for any expression whose value is that type. The most important examples of string expressions are text constants (literals) and variables that contain text.

Printing text

The strings are printed in the same way as the numeric data. The string we want to print is simply specified as a print() function argument.

When the print() function has multiple arguments, these arguments can be of different types:

When we use multiple arguments, we write them separated by commas (as with any function). The values of all the arguments specified will be printed in the same order, and will be separated by one space.

More about printing numbers

Sometimes the printed result looks illegible:

Most often we don’t need all these digits. Real numbers can look more readable if we use the format function. With this function (among other things) we can specify how many digits after the decimal point we want to display:

To specify the number of decimal places to display, we called the format function like this: the first argument of the function is the value we print, and the second argument is the description of the printing format. In this description, the part ‘.2’ means that we want two decimal places, and the part ‘f’, abbreviated from float, means that we give a description for a real number (the type of real numbers is called float). The function returns a string in which the number x is written as specified.

Note that this formatted printing does not change the value of the variable x.

We’ve broken the example down into steps to make it clearer, though it could also be written in one line of code. For example, to print with 4 decimal places:


When displaying multiple real numbers one below the other, we can make them more readable by aligning the decimal points. For example, this way of printing is not easily comprehensible:

To get a more readable look, we can use the format function like this:

In the description ‘8.2f’ the number 8 means that the textual version of the given number should be left-padded with spaces (if needed) to take up total of 8 places. These 8 places include the digits, the decimal point, possible sign of the number and spaces in front of the number.

Other parts of the description (‘.2f’) have the same meaning as before.

The format function has many other features, but we will not use them here.

String operations

Joining strings

Strings can be joined together with a string concatenation operation. This operation is denoted by the sign +, just like the operation of summation, so in programming concatenation is often informally called string addition.

Sometimes, we may have an integer or a real number wiitten in a string, so it is important to understand when the sign + refers to the addition of numbers, and when concatenation of strings. For example, in the following program, the first a + b is the addition of numbers, and the second is the addition of strings. Accordingly, the printed results also differ (try it out).

It is likely that occasionally you may be confused by the result when executing a program. The result may be different than expected for many reasons, and one possibility is that you unintentionally added up strings instead of numbers.

The + character can stand between two numeric expressions or between two strings, but not between a string and a number (in any order). Such combinations result in a TypeError (try it).

String multiplication

Strings can also be multiplied. This means that it is allowed to multiply a string by an integer (either from left or right), and the result is a new string, which is obtained by repeating a given string a given number of times.

In the following example we underline the numbers with a line, and that line is obtained as a result of multiplying the string ‘-‘ by 12.

Questions and Tasks

    Q-9: Match the *format* function calls with the results. The spaces are represented by '_' as they would not otherwise be visible. Try again!
  • '12.34'
  • format(12.34, '.2f')
  • '__12.34'
  • format(12.34, '7.2f')
  • '_12.34'
  • format(12.34, '6.2f')
  • '__12.3'
  • format(12.34, '6.1f')
  • '12.3'
  • format(12.34, '.1f')

    Q-10: Which of the statements is faulty?

  • s = 'a' + "b"
  • Try again
  • s = 'ab"
  • Correct!
  • s = 'ab'
  • Try again

    Q-11: Which statement prints `` tra-la-la ‘’? (Mark all correct answers)

  • print('tra' + 2 * '-la')
  • print('tra-' + 2 * 'la-')
  • print('tra-' + 'la-' + 'la')
  • print('tra-' + 'la-la')
  • print('tra-la-' + '-la')
    Q-12: Match expressions with their values. Try again!
  • 'NA' * 3
  • 'NANANA'
  • 'N' + 3 * 'A'
  • 'NAAA'
  • 'N' * 3 + 'A'
  • 'NNNA'
  • 'N' * 3 + 3 * 'A'
  • 'NNNAAA'

What the statement print((‘N’ + ‘A’) * 2) prints?

Task - Profit Sharing

The three friends agreed to divide the profits from the joint venture so that the first would get 2/7 of the earnings, the second 1/3, and the third the remaining sum. The total profit was 40000. Complete the program, which will print, in two decimal places, the earnings of each of the three friends.