Python-Strings-Review(2)


Good morning boys

I hope the various data types available in Python are clear to you now.
I am taking another revision session of the String and its slicing during this session.

------------------------------------------------------------------------------------------------------------
Google meet link for today:


-------------------------------------------------------------------------------------------------------------------
By the end of this session , you will be able to
  • understand the internal indexing of the strings
  • Apply the understanding and predict the output for given questions
  • Apply the concept of slicing in Python strings.
----------------------------------------------------------------------------------------------------------------
How Strings are Indexed
Each of a string’s characters correspond to an index number, starting with the index number 0.
For the string Sammy Shark! the index breakdown looks like this:
SammyShark!
01234567891011
As you can see, the first S starts at index 0, and the string ends at index 11 with the ! symbol.
We also notice that the whitespace character between Sammy and Shark also corresponds with its own index number. In this case, the index number associated with the whitespace is 5.
The exclamation point (!) also has an index number associated with it. Any other symbol or punctuation mark, such as *#$&.;?, is also a character and would be associated with its own index number.
The fact that each character in a Python string has a corresponding index number allows us to access and manipulate strings in the same ways we can with other sequential data types.

Accessing Characters by Positive Index Number

By referencing index numbers, we can isolate one of the characters in a string. We do this by putting the index numbers in square brackets. Let’s declare a string, print it, and call the index number in square brackets:
ss = "Sammy Shark!"
print(ss[4])
Output
y
When we refer to a particular index number of a string, Python returns the character that is in that position. Since the letter y is at index number 4 of the string ss = "Sammy Shark!", when we print ss[4] we receive y as the output.
Index numbers allow us to access specific characters within a string.

Accessing Characters by Negative Index Number
If we have a long string and we want to pinpoint an item towards the end, we can also count backwards from the end of the string, starting at the index number -1.

For the same string Sammy Shark! the negative index breakdown looks like this:
  • S
  • a
  • m
  • m
  • y

  • S
  • n
  • a
  • k
  • e
  • !
  • -12
  • -11
  • -10
  • -9
  • -8
  • -7
  • -6
  • -5
  • -4
  • -3
  • -2
  • -1



By using negative index numbers, we can print out the character r, by referring to its position at the -3 index, like so:
  1. print(ss[-3])









Using negative index numbers can be advantageous for isolating a single character towards the end of a long string.

Slicing Strings
We can also call out a range of characters from the string. Say we would like to just print the word Shark. We can do so by creating a slice, which is a sequence of characters within an original string. With slices, we can call multiple character values by creating a range of index numbers separated by a colon [x:y]:
print(ss[6:11])
Output
Shark
When constructing a slice, as in [6:11], the first index number is where the slice starts (inclusive), and the second index number is where the slice ends (exclusive), which is why in our example above the range has to be the index number that would occur just after the string ends.
When slicing strings, we are creating a substring, which is essentially a string that exists within another string. When we call ss[6:11], we are calling the substring Shark that exists within the string Sammy Shark!.
If we want to include either end of a string, we can omit one of the numbers in the string[n:n] syntax. For example, if we want to print the first word of string ss — “Sammy” — we can do so by typing:
print(ss[:5])
Output
Sammy
We did this by omitting the index number before the colon in the slice syntax, and only including the index number after the colon, which refers to the end of the substring.
To print a substring that starts in the middle of a string and prints to the end, we can do so by including only the index number before the colon, like so:
print(ss[7:])

Output
hark!
Now Let us test our knowledge:


str = 'Computer Applications'

print str          
print str[0]       
print str[3:7]     
print str[9:]      
print str[-1]
print str * 2      
print str + "Rocks!"

I request you to share your answers.

Comments

Post a Comment

Popular posts from this blog

HOLIDAY HW