String indexing and Slicing in Python

 String indexing and Slicing in Python

String indexing and Slicing in Python

  • String indexing allow us to access individual characters in a string.
  • We can do this by using square brackets and the location,or index,of the character you want to access.
  • It’s important to remember that Python starts indexes at 0.So to access the first character in a string,you would use the index[0].
  • If we try to access an index that’s larger than the length of your string,you’ll get and Index Error.
  • This is because you’re trying to access something that doesn’t exist.
  • We can also access indexes from the end of the string going towards the start of the string by using negative values.
  • The index[-1] would access the last character of the string,and the index[-2] would access the second-to-last character.
  • We can also access a portion of a string,called a slice or a sub string.This allows you to access multiple characters of a string. 
  • We can do this by creating a range,using a colon as separator between the start and end of the range,like[2:5].
  • This range is similar to the range () function.It includes the first number,but goes to one less than the last number.
  • We can also easily reference the start or end of the string by leaving one value blank.For example[:5] would give us all characters from the start to the fourth character in the string.
  • We can also use [5:] to get the characters from the fourth character to the end of the string.

https://www.technologiesinindustry4.com/2020/10/ethereum-erc20.html

1 thought on “String indexing and Slicing in Python”

Leave a Comment