Use Of Strings In Python Language

Welcome back, in today’s tutorial we will be discussing on use of strings in Python language, in this tutorial we look into the following parts giving out the relevant examples;

What we will look at?

  • Introduction to strings
  • How to format strings
  • How to modify strings
  • String concatenation
  • String slicing

Let’s get started with strings

String – refers to data values that are composed of characters that are in particular order for instance you may a string as Hey, I am learning Python, in python we uses print() function to show a string where it is written in between the parentheses enclosed with a single or double quotes as shown in the example below;

print("Hey, I am learning Python")
print('Hey, I am learning Python')

Defining strings in a variable

In other cases you can define a certain string in a variable then out put it using print() function, look at this example here;

x = "Hey, I am learning Python"
print(x)

How a define string with several lines?

In other cases you may find that you have a string with multiple lines and you want to define it in a variable. Therefore, we uses either three double quotes(“””) or even three single quotes(”’). Look at this example below;

x = """Hello world, I am learning use of strings in python,
When defining a string with several lines we uses three double or single quotes, 
python i am finding it so easy"""
print(x)


#remember you can use three single quotes instead

How do you loop through a string?

You can also loop through a string as we had discussed in our tutorial on for loops, look at this example below;

for y in "programming"
 print(y)

How do you find the length of a string?

We uses len() function to find the length of the a string, see the example below;

x = "Hey, I am learning Python"
 print(len(x))

How do I check for a word or character in a string?

Did you know that you can check for a word or even character in a string? in this case we uses in keyword, then we may apply use of if statement, look at this example below;

hobby = "I love doing coding during my leisure time"

if "love" in hobby:

 print("The word was found in the string!")

How do I check whether a certain word or character is not present in a string?

In other cases you may use not in keyword to check whether a certain word or even character is not present in a particular string. Look at this example;

hobby = "I love doing coding during my leisure time"

if "python" not in hobby:

 print("The word was not found in the string!")

Use of strings as Arrays

As we had discussed in our tutorial on Arrays, strings are arrays whereby each character represents a length of 1. You may read on our tutorial on arrays to understand more, arrays start counting from 0. Therefore, the first character has a value of 0 . Look at this example here;

x = "Hey, i am learning Python"

 print(x[2])

How To Modify Strings

This is the second part of our tutorial, we will see how you can modify strings using various built-in methods as follows;

  • Changing to uppercase or lower case

You can change a string to either uppercase or even lower case using upper() and lower() methods respectively; look at these examples here;

case 1,
x = "Hey, I am learning Python"
print(x.upper())


case 2,
x = "Hey, I am learning Python"
print(x.lower())
  • Replacing a string with another

We uses replace() method when you want to replace a string with another, look this example here;

x = "Hey,I am learning Python"
print(x.replace("Python is so easy"))
  • How do you split a string?

We uses split() method when you want to split a string into other substrings if there are anywhere we have a separator. For better understanding let’s have an example here;

x = "Python, language"
print(x.split(","))

It will produce this;
['Python', 'language']

How To Format Strings

In this part we will discuss how we can use format() method to combine a string and a number, since it will be impossible to combine a string a number as we discussed in our tutorial on variables you can refer back. The format() method makes it possible as it formats the arguments and places them in the string. Look at this example here;

year = 1999
phrase = "I was born in the year {}"
print(phrase.format(year))

#it places the number where we have {}

-Remember you can use unlimited number of arguments, where also you can use {0} to make sure you have placed the arguments in the right place. Let’s have an example here;

yr1 = 1999

yr2 = 2004

bornyr = "I was born in {} while my sister was born in {}"

print(bornyr.format(yr1, yr2))

Python Slicing

In this part we discuss python slicing whereby we will see how you get the range of characters by using the slice syntax, the default starting point is 0, therefore you need to include the range e.g 3:8 otherwise it will start from 0. Look at this example;

x = "Hey, I am learning Python"
 print(x[3:9]) 

#remember 9 is not included.

-There are various ways in which you write your range values as follows;

  • Slicing from the end e.g (:7) it slices from 0 by default
  • Slicing from the start e.g (3:) it slices from position 3
  • Negative slicing where it slices from the end e.g (-3:-6)

String Concatenation

This marks our last part of our tutorial today, we will be discussing how you can combine two or more strings by using + sign. First you will need to define the variables then combine them using + sign. Look at this example below;

x = "Hey, I am learning Python"
y = "It seems so easy"
z = x + y
print(z)

#you can insert the spaces between the strings by using separate empty double quotes(" ") as shown below;
z = x + " " + y

Thanks for reading through our tutorial, hope now you understand use of strings in python language. As we continue learning about Python hoping to meet in our next tutorial..