[source code] Python Programming Tutorial - 4 - Slicing up Strings

+3 Bucky Roberts · August 26, 2014

>>> user = "Tuna McFish"
>>> user[0]
'T'
>>> user[5]
'M'
>>> user[-1]
'h'
>>> user[-3]
'i'
>>> user[2:7]
'na Mc'
>>> user[:7]
'Tuna Mc'
>>> user[2:]
'na McFish'
>>> user[:]
'Tuna McFish'
>>> print('dsadasd')
dsadasd
>>> len('dfjhf22893hfsdfjkasdf')
21
>>> len(user)
11

Post a Reply

Replies

Oldest  Newest  Rating
0 Gary Murphy · August 27, 2014
Hey Bucky (or whoever wants to answer this question),
In your video you talked about using [:] to print the entire string.
Is there a difference between using:
print user


and

print user [:]


?
I know when you assign variables like:

beans = potato[:]


beans will be given a new place in memory with the same value as potato, so should the value of potato change beans will not change with it.
But as far as printing goes, is there any benefit to including the [:]?
  • 1

Python

107,039 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator