Strings and Slicing

+1 Jerry Tagle · November 30, 2014
Hello, I'm still very new to programming using Python, just started yesterday.
In this example:

User= "Tuna McFish"

1. What's the code if you just want show 'T McFish'?
2. Also, using print() function, using variable 'user', how to show 'Tuna' then next line is McFish? That looks like this one below:
Tuna
McFish

Thanks! Just exploring each tutorial deeper and very addicting but I got stuck with those two problems. Thanks in advance! :)

Post a Reply

Replies

Oldest  Newest  Rating
0 Doug Fresh · December 1, 2014
Hello Jerry, welcome to the community!

All strings have a number of characters in them, beginning with the number 0, and continuing for the length of the string. 
user = "Tuna McFish" has 11 characters.  A demonstration of this would be: 


user = "Tuna McFish"

for character in user:
    print(character)

print(" \n end of first example \n")

## we set character equal to an integer, instead of
## the character in the string.
    
## range sets a number range between 0 and whatever value
## is indicated within the first set of parenthesis

## len(user) represents the maximum number of characters in a string,
## in this case 10, (starting at 0)

for character in range(len(user)):
    print(character)
print("\n end of second example \n")

## now that the positions are known, we can choose numbers
## that refer to the position of each character in the string

for character in range(len(user)):
    print(user[character])
print("\n end of third example \n")

## you'll notice that the third example and the first example
## had identical output when printed
## So, we know we can refer to a position and return a character from a string,
## now let's put everything together to create a "slice"

print(user[0], user[4:])
print("\n end of the fourth example \n")

## the colon : allows a programmer to create an index.
## If you wanted to display the something like "McFish Tuna McFish"
## or even "Fun shiT", it can be done simply by referencing the characters
## in a string.

print(user[5:], user[0:4], user[5:])

## you'll notice when you specify an index such as [0:4] only the character values 0, 1,2,3
## will be included, you will have to add one to include the 4th character,
## this is shown below with user[9:11]. Even though there are only 10 positions
## (Beginning from 0)-- we increase the maximum value by one to include the 10th position.

print(user[7] + user[1:3] + " " + user[9:11] + user[8] + user[0])


Copy and paste the code into a new python program/text editor. If you wish to run an individual section,  you can prevent a section of code from running by commenting it out, with a #.  The cool thing about python is there are many ways to skin a cat. Have fun with it, and if you want even more practice with python, try codingbat.com. It has fun exercises that can be a little challenging.
  • 1

Python

107,232 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator