String Functions

Let's go through review, where we understand how conditionals works. How to apply if-function. How to write algorithms, where Computers make decisions, based on the user request.

1/5
Caption Text
2/5
Caption Text
3/5
Caption Text
4/5
Caption Text
5/5
Caption Text

Python has a set of built in functions that you can use on strings. Remember, All string functions returns new values. They do not change the original string.

What is string in Python?


As we discussed earlier in previous chapter, String is a sequence of characters. A character is simply a symbol. For example, the English language has 26 characters.
Computers donot deal with characters, they deal with numbers(binary). Even though you may see characters on your screen, internally it is stored and manipulated as a combination of 0's and 1's. This conversion of character to a number is called encoding. Which you will study in further details in future.

Python strings Operations.

There are many operations that can be performed with strings which makes it one of the most used data types in Python. We will discuss some string functions in this chapter and other many more string functions will be discussed in further detain in coming chapters.

String Functions:
  • in
  • len()
  • .lower() & .upper()
  • .islower() & .isupper()
  • and many more!

in

in: -- in returns True if the first string is contained in the second string. For instance:

  • "ello"  in  "Hello" ------->  True 
  • "UPPER" in  "supper" ------>  False
  •  " "    in  "Hi there!" ----> True
  •  "bear" in  "ear"   --------> False

len(some_str)

len returns the number of characters in the given string:

  • len("hello") ----> 5
  • len("")-----> 0
  • len(" ")-----> 1
  • len("test") >= 5 ----> False

Some_str.lower() & Some_str.upper()

.lower() returns a copy of the string in all lowercase. But it doesnot odify the original string.

  • "Hello".lower() -------> hello
.upper() returns a copy of the string in all uppercase, but it doesnot modify the original string.
  • "Hello".upper()-----> HELLO

some_str.islower() & some_str.isupper()

.islower() returns whether the string contains ONLY lowercase characters.

  • "Hello".islower() ----> False

.isupper() returns whether the string contains ONLY uppercase characters.

  • "HELLO".isupper()-----> True

There are many more string functions that are useful. For instance: .isdigit(), .isalnum(), .find() and many more.

How to access characters in a string?

We can access individuals characters using indexing and a range of characters using slicing. Index starts from 0. Trying to access a character out of index range will raise an IndexError. Python allows negative indexing for its sequences. For instance; the index -1 refers to the last item, -2 to the second last item and so-on. We can access a range of items in a string by using the slicing operator:(colon).

#Accessing string  characters in python:
                        str = "programming";
                        print("str = ", str);

                        #access first character
                        print("str[0] = ", str[0]) -----> p

                        #access last character
                        print("str[-1] = ", str[-1]) -----> g

                        #Slicing 2nd to 5th character
                        print("str[1:5] = ", str[1:5]) ----->rogr

                        #slicing 6th to 2nd last character
                        print("str[5:-2] = ", str[5:-2]) ---->ammi
                    
***If you try to access an index out of range or use numbers other than an integer, we will get errors.
                        #index must be in range
                        print(str[15])-----> Index Error, string out of index.
                        #index must be an integer.
                        print(str[1.5])-----> Index Error, string indices must be integers.