Javaexercise.com

Python String and Substring

Python is a high-level, interpreted programming language, and is one of the most popular programming languages present in the software industry. One of the most important but often overlooked concepts in python is that of strings. Let us have a look at them in detail.

What is a String in Python?

In python, a string, just like other programming languages, is a common data type that is usually used to represent text.

Strings are among the most used types in python. Unlike other languages, there is no different character data type available in python. A character is simply represented as a string of length 1.

Syntax of String in Python

The syntax of representing is very straightforward and is as follows. We simply enclose the required string within quotes. These might be single quotes ‘ or double quotes “ 

‘<required_string>’ or “<required_string>”

Creating and assigning strings in Python

This is a very straightforward example. Just like other data types, we can also print strings and assign them to other variables.

As discussed earlier, we use either single quotes or double quotes to represent strings and have demonstrated both below.

Let us look at the python code and corresponding output for this example:

# Creating string using single quotes
a = 'Hello'

# printing string a
print(a)

# Creating string using double quotes
b = "Hello"

# printing string b
print(b)

# print string
print('Hello')
print("Hello")

Output

Hello

Hello

Hello

Hello

Accessing individual characters of the string in Python

A string can be simply considered as an array of individual characters in some sense.

Thus, we can similarly access individual elements or characters of a string in a similar manner, by using square brackets [ ] along with the zero-based index of the required character.

Here, the string is Hello and the character accessed is e with index 1.

Let us look at the python code and corresponding output for this example:

# Creating string
b = "Hello"

# printing char of 1 index in the  string b
print(b[1])

Output

e

Accessing substrings in Python

Substrings refer to a subset of characters present in the string.

You can access substrings in a manner similar to how you access individual characters, that is, by using square brackets [ ].

We specify exactly what substring we need by giving the starting and ending indices.

One important thing to note here is that the ending index is not included.

So if we have a string Hello and want to access the characters having indices 1,2 and 3, that is e l l, we do it using 1:4.

Let us look at the python code and corresponding output for this example:

# Creating string in python
b = "Hello"

# Accessing substring in python
print(b[1:4])

Output

ell

Get the Length of a string in Python

In some cases, we might need to find the length of the string for other purposes.

We can do this by simply using the len() function and passing the string or the variable that the string has been assigned to as a parameter for this function.

Let us look at the python code and corresponding output for this example:

# Creating String
b = "Hello"

# printing length of string
print(len(b))

Output

5

Multiline strings in Python

Instead of restricting strings to a single line, we can also have strings that span multiple lines for different use cases.

This is done by enclosing the intended string in three pairs of double quotes instead of just one pair of double-quotes.

Let us look at the python code and corresponding output for this example:

# Creating multiline string in Python
b = """Hello

World"""

# printing multiline string in python
print(b)

Output

Hello

World

Instead of using three pairs of double quotes to denote a multi-line string, we can also use three pairs of single quotes.

Let us look at the python code and corresponding output for this example:

# Creating multiline string in python
b = '''Hello

World'''

# printing multiline string in python
print(b)

Output

Hello

World

Looping over a string - Iterate String in Python

In some cases, we might need to loop over the string character by character. We can do this using a for loop.

In this example, we have a string Hello assigned to a variable b.

We loop over this string and print each character on a different line.

Let us look at the python code and corresponding output for this example:

# Create a  string
b = "Hello"

# iterate string chars in python
for i in b:
  print(i)

Output

H

e

l

l

o

Conclusion

In this topic, we have learned the use and advantages of strings in a Python program, following a running example of a simple creating and printing string, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to [email protected] in case of any suggestions.