1. Strings¶
1.1. Printing strings¶
>>> print('Hello World!')
Hello World!
Or using double quotation mark
>>> print("Hello World!")
Hello World!
Note
Single ‘ quotation and Double ” quotation works almost same.
But if you want to put '
into quotation mark you need to mix quotations characters ex.
>>> print("It's a nice day")
It's a nice day
But if you use '
twice you will see syntax exception
>>> print('It's a nice day')
Traceback (most recent call last):
SyntaxError: invalid syntax
1.3. Checking types¶
>>> type(txt)
<class 'str'>
1.4. Checking length of string¶
>>> len(txt)
12
1.5. Printing special characters¶
New line special character
>>> print("Hello\nWorld!")
Hello
World!
Tabulator character
>>> print("Hello\tWorld!")
1.6. String concatenation¶
>>> print('Hello ' + 'attendee')
Hello attendee
1.7. String concatenation - format¶
>>> print('Hello {}, have a great day'.format('Tomasz'))
Hello Tomasz, have a great day
1.8. Different representations - format¶
>>> '{:s}'.format('Some text') # in case of digit - exception
'Some text'
>>> '{:s}'.format(4) # in case of digit - exception
Traceback (most recent call last):
ValueError: Unknown format code 's' for object of type 'int
class Data:
"""Simple Data class"""
def __init__(self, value):
self.value = value
def __str__(self):
return '{}'.format(self.value)
def __repr__(self):
return '<{} object with value: {}>'.format(self.__class__.__name__, self.value)
print("{0!s}".format(Data(54), Data(54)))
print("{0!r}".format(Data(54), Data(54)))
print("{obj!s}".format(obj=Data(41)))
print("{obj!r}".format(obj=Data(41)))
54
<Data object with value: 54>
41
<Data object with value: 41>
>>> '{:>10}'.format('test')
' test'
>>> '{:10}'.format('test')
'test '
>>> '{:^10}'.format('test')
' test '
1.10. Substrings¶
>>> 'Hello'[-1]
'o'
>>> 'Hello'[0:6:2]
'Hlo'
names = 'Marta, Kasia, Monika, Tomek, Przemek, Janek, Marta, Malgosia'
print(names.count('Ma'))
In result we receive number of occurrences
3
Below we find an index of string.
>>> names.find('Kasia')
7
Hint
Letter ‘K’ is at 8th position, which means index no. 7 (countring from 0)
1.11. Splitting strings¶
>>> names.split(',')
['Marta', ' Kasia', ' Monika', ' Tomek', ' Przemek', ' Janek', ' Marta', ' Malgosia']
We received list of strings
1.12. Operations of strings¶
>>> names = names.replace("Janek", "Adam")
>>> print(names)
Marta, Kasia, Monika, Tomek, Przemek, Adam, Marta, Malgosia
1.13. Checking if string is a digit¶
>>> names.isdigit()
False
>>> temperature = "34"
>>> print(temperature.isdigit())
True
1.14. String as uppercase¶
>>> print(names.upper())
MARTA, KASIA, MONIKA, TOMEK, PRZEMEK, ADAM, MARTA, MALGOSIA
1.15. String as lowercase¶
>>> print(names.lower())
marta, kasia, monika, tomek, przemek, adam, marta, malgosia
1.16. Exercise¶
1 Create program writing your name and surname 2 Print fallowing statement: “Test characters: ‘, /, ” ” 3 Create two attendees of this training (give them names which you like) (both attendees are seperate variables)
first_attendee,
second_attendee
4 Exchange places of attendees - first_attendee
should have second_attendee
content and other way round
Print attendees,
Is it possible to change places in different way ?
5 Let user put his name using keyboard (Use google)