Python- Strings
Introduction to Strings:
- Strings are used in Python to record text information, such as names. It could either be a word, a phrase, a sentence, a paragraph, or an entire encyclopedia. Strings in Python are actually a sequence, which basically means Python keeps track of every element in the string as a sequence.
- For example, Python understands the string "joker' to be a sequence of letters in a specific order. This means we will be able to use indexing to grab particular letters (like the first letter, or the last letter).
- This idea of a sequence is an important one in Python and we will touch upon it later on in the future.
Creating a String:
To create a string in Python you need to use either single quotes or double-quotes.
Example:
my_first_string= 'algebra'
or
my_first_string = "algebra"
How to print strings?
Using Jupyter notebook with just a string in a cell will automatically output strings, but the correct way to display strings in your output is by using a print function.
Python's built-in - len() function counts all of the characters in the string, including spaces and punctuation.
String Indexing:
- We know strings are a sequence, which means Python can use indexes to call parts of the sequence.
- A string index refers to the location of an element present in a string.
- The indexing begins from 0 in Python.
- The first element is assigned an index 0, the second element is assigned an index of 1, and so on and so forth.
- In Python, we use brackets "
[]",after an object to call its index.
String Slicing:
- We can use a
:to perform slicing which grabs everything up to a designated point.
- The starting index is specified on the left of the
:and the ending index is specified on the right of the:.
- Remember the element located at the right index is not included.
Example:
Grab everything past the first term all the way to the length of s which is len(s)
string = 'Principal Component Analysis!'
print(string)
it prints:
Principal Component Analysis!
print(string[:13])
it prints:
Principal Com
print(string[10:])
it prints:
Component Analysis!
print(string[3:5])
it prints:
nc
>>> If you do not specify the ending index, then all elements are extracted which comes after the starting index including the element at that starting index. The operation knows only to stop when it has run through the entire string.
>>> If you do not specify the starting index, then all elements are extracted which comes before the ending index excluding the element at the specified ending index. The operation knows only to stop when it has extracted all elements before the element at the ending index.
>>> If you do not specify the starting and the ending index, it will extract all elements of the string.
Example:
string = 'Principal Component Analysis!'
print(string[:])
print(string)
it prints:
Principal Component Analysis!
Principal Component Analysis!
>>> We can also use negative indexing to go backward.
Last letter (one index behind 0 so it loops back around)
string = 'Principal Component Analysis!'
print(string[-29])
it prints:
'P'
String functions and methods:
len()
len()the function returns the length of the string.
lower()
lower()the method converts the string to lowercase.
upper()
upper()the method converts the string to uppercase
count()
count()the method returns the count of a string in the given string. Unlikelower()andupper()method, thecount()the method takes a string as an argument.
find()
find()the method returns the index of the first occurrence of a string present in a given string. Similar to thecount()method, thefind()the method takes a string as an argument.
replace()
replace()the method takes two arguments - (i) the string to replace and (ii) the string to replace with, and returns a modified string after the operation.
Also, You can read ...
No comments:
Post a Comment