All IT Courses 50% Off
Python Tutorials

What are Python Strings ?

Python Strings: Replace, Join, Split, Reverse, Uppercase & Lowercase

In the previous article, we have seen what a variable is. Python strings are variables with data type “str”. Anything written within double or single quotes is taken as a string in Python. Let us learn by doing.

Declaration of String

In the code below “Hi! I am string” is stored in the variable name ‘string’, and then the string is printed.

What are Python Strings ?

Let us see how to check the data type of a variable.

Simply write type(variable_name). It will return the data type of variable. As it can be seen below that the data type of string variable is “str”.

All IT Courses 50% Off

What are Python Strings ?

How to access values in the string?

‘We access values in a string using an index number. 

For example If “Hello” is our string and we want to access “H” we will use variable_name[0]. It will return us “H”

What are Python Strings ?

What are Python Strings ?

Range variable-name[:]

We can provide a range to access more than one value at the same time. Take a look at the example below.

H -> 0
E -> 1
l  -> 2
l  -> 3
o -> 4

string[1:3]  means that start from 1 and get all the values until 3.

What are Python Strings ?

in Operator

In operator is used to find one or more letters in the string. It returns True if found otherwise False.

What are Python Strings ?

Replace

If we wish to replace one or more letters in a string we can use built-in replace function.

Let say you have a string “I like python” and now you want it to be “I love python”.

NOTE:- The new string will be stored in the new variable. The string data type is immutable means that if you once declared a string it cannot be changed. 

What are Python Strings ?

JOIN Function

We will take a simple example of join because its bases on the concepts we have not yet covered to try to just learn how it works you will understand its benefit after the learning list concept.

For example, you want to add a star ‘ * ’ in the “birthday” string to make it look nice when printed. How would you do this? Let’s see in pycharm.

What are Python Strings ?

Oh, that looks like a fancy birthday.

Split function

You have a string “Happy Birthday”, and you want to separate/split “Happy” and “Birthday”. You will use the split function for this functionality. Take a look at the example.

What are Python Strings ?

Reverse Function

If you want to reverse a string, “Hello” to “olleH”, then use a reverse function.

Let’s take a look at code. 

The reversed function is a generator. It will be explained in the upcoming articles.

What are Python Strings ?

Uppercase & Lowercase

If you wish to convert your string to uppercase or lowercase simply use:

string.upper()

string.lower()

What are Python Strings ?

 

 

 

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button