Sequence Data types part 1-strings

In Python, sequences are a group of data types that can be iterated(looped) and support element access using integer indices. That means we can access the elements of a sequence using index values which start from 0. Sequences also support the len() function which returns the length(total number of characters or elements) of the sequence. Examples of sequence data types are str, list and tuple. Let us go through them one by one.

Strings

In Python, text is represented as strings. They are enclosed either within single or double quotes. For example,

Strings can be a single character, one or  more words, sentences etc.

In Python, strings are of type str.

Actually, strings come under a kind of data type known as sequence types which includes strings, lists and tuples.

Manipulating strings

Strings can be joined or concatenated using the + operator.

For example,

The + operator can also be used to embed a variable value into another string.

However, this becomes more readable if we use f-strings. For example,

f-strings or formatted strings use this curly brace syntax to denote variables which is also known as string interpolation.

Apart from this, strings can also be repeated using the * operator. For example,

The individual characters of a string can be accessed by index values. The index values start from 0 for the first character, 1 for the second character and so on until the end of the string.

This way, we can access each character of a string.

This also supports negative indices. An index of -1 points to the last character of a string, -2 points to the second last character and so on.

Python also supports string slicing. Slicing helps us to extract more than one character(substring) from a string.

Slicing helps us to extract a substring from a given string according to a pair of index values. Please note that the extracted substring starts from the first specified index up to the second specified index but not including the second index. For example, for s[0:2], the extracted substring would start from index 0 to index 2 but not including index 2, which means the substring would be from index 0 to 1.

If we omit the first index, it starts from index 0. If we omit the second index, it ends at the last index for that string. For example,

There is also a len() function which gives us the total length(total number of characters) of the string.

Basic string methods

Let us discuss some string methods which will help us to convert or manipulate strings in Python. As we already know, strings are immutable which means they cannot be modified. That’s why none of these string methods change or modify the original string. Instead, they always return a new string with the changed or modified characters.

str.upper()

The upper() method helps us to transform any string to uppercase.

This method converts all the characters of a string to uppercase. Please note that it doesn’t change the original string. Instead, the upper() method returns a new string with all characters changed to uppercase.

str.lower()

The lower() method returns a new string with all characters converted to lowercase.

Just like the upper() method, it doesn’t change or modify the original string.

str.capitalize()

The capitalize() method returns a new string with its first character capitalized and the rest lowercased.

str.startswith(prefix)

The startswith() method checks whether a given string starts with a specified prefix or not. It returns True if the given string starts with the specified prefix, otherwise returns False.

Here, we can see that the string “edupoly” starts with “e”. So, we get True in the first print statement output. But the string “welcome” doesn’t start with “a”. So, we get False in the second print statement output.

str.endswith(prefix)

The endswith() method checks whether a given string ends with a specified suffix or not. It returns True if the given string ends with the specified suffix, otherwise returns False.

Here, we can see that the string “edupoly” doesn’t end with “e”. So, we get False in the first print statement output. But the string “welcome” ends with “e”. So, we get True in the second print statement output.

str.find(sub)

The find() method returns the first index in the string at which the substring sub is found. It returns -1 if sub is not found in the given string.

Here, in the string “banana”, the first occurrence of the character “n” is at index 2. That’s why we get the output as 2 for the first print statement. But the substring “f” is not present in the string “banana”. So, we get the output as -1 for the second print statement.

Interview Questions

Q. How are strings represented in Python, and what are some examples of different types of strings?

Ans. In Python, strings are represented as sequences of characters enclosed within either single or double quotes. They can consist of a single character, one or more words, sentences, or even larger blocks of text. 

Examples:

  • ‘a’ (single character)
  • “Hello” (word)
  • “This is a sentence.”
  • ”’This is a multi-line string spanning multiple lines.”’

Q. Explain how string concatenation and embedding variable values work in Python. What are the different ways to achieve this?

Ans. String concatenation is the process of joining two or more strings together. In Python, you can concatenate strings using the + operator:

greeting = “Hello”

name = “Alice”

message = greeting + “, ” + name + “!”  # message is “Hello, Alice!”

You can also embed variable values into strings. This can be done using the + operator along with type conversion (less readable), or more preferably using f-strings (formatted strings):

age = 30

message = f”Happy {age}th birthday!”  # message is “Happy 30th birthday!”

Q. How can you access individual characters and substrings within a string using indexing and slicing?

Ans. Individual characters in a string can be accessed using their index values, which start at 0 for the first character. Negative indices can be used to access characters from the end of the string (-1 for the last character, -2 for the second-to-last, and so on).

text = “Python”

first_char = text[0]       # first_char is ‘P’

last_char = text[-1]      # last_char is ‘n’

Slicing is used to extract a substring from a string. It uses the syntax string[start:end], where the resulting substring includes characters from the start index up to (but not including) the end index.

text = “Python”

substring = text[0:4]     # substring is ‘Pyth’

Q. What is the purpose of the len() function in the context of strings, and how is it used?

Ans. The len() function returns the total length of a string, which is the number of characters it contains. It can be used like this:

text = “Hello, world!”

length = len(text)        # length is 13

Coding Problems

Q. Write a program to extract the first and last characters of a string and print them with a space in between?

Ans

text = “Python”

first_char = text[0]  # Access first character

last_char = text[-1]   # Access last character

print(first_char + ” ” + last_char)  # Output: P n

Q. Write code to check if a string starts with “Hello” and ends with “world!”.

Ans

text = “Hello, world!”

starts_with_hello = text[0:5] == “Hello”

ends_with_world = text[-6:] == “world!”

print(“Starts with ‘Hello’:”, starts_with_hello)

print(“Ends with ‘world!’:”, ends_with_world)

Q. Write code to capitalize the first letter of each word in a sentence.

Ans

sentence = “this is a sentence”

capitalized_sentence = “”

capitalize_next = True

for char in sentence:

  if capitalize_next:

    capitalized_sentence += char.upper()

  else:

    capitalized_sentence += char

  capitalize_next = char == ” “

print(capitalized_sentence)  # Output: This Is A Sentence

Q. Write code to count the occurrences of the letter ‘e’ in a string.

Ans. 

text = “Hello, there!”

e_count = 0

for char in text:

  if char == “e”:

    e_count += 1

print(“Number of ‘e’ characters:”, e_count)

Q. Write code to create a new string where letters alternate between uppercase and lowercase.

Ans.

text = “hello”

alternating_case = “”

is_upper = True

for char in text:

  if is_upper:

    alternating_case += char.upper()

  else:

    alternating_case += char

  is_upper = not is_upper

print(alternating_case)  # Output: HeLlO

Q. Reverse a String

Ans

text = “Python”

reversed_text = “”

for index in range(len(text) – 1, -1, -1):

    reversed_text += text[index]  

print(reversed_text)  # Output: nohtyP

Q. Count Vowels

Ans.

text = “Hello, world!”

vowel_count = 0

vowels = “aeiou”

for char in text.lower(): 

    if char in vowels:

        vowel_count += 1

print(vowel_count)  # Output: 3

Q. Repeat a Substring

Ans

text = “Ha”

substring = text

repeat_count = 3

repeated_string = “”

for _ in range(repeat_count):

    repeated_string += substring  # Concatenate substring repeatedly

print(repeated_string)  # Output: HaHaHa

Code snippet based problems

Q. What will this code snippet print?

mystery_string = “banana”

result = “”

for i in range(0, len(mystery_string), 2):

    result += mystery_string[i]

print(result) 

Ans. This code will print bnn. The loop iterates over the string with a step of 2, extracting and appending every other character starting from the first.

Q. What will this code snippet print and what does it do?

word = “racecar”

is_palindrome = True

for i in range(len(word) // 2):  

    if word[i] != word[-(i+1)]:

        is_palindrome = False

        break

print(is_palindrome)

Ans. This code snippet will print True and it checks whether the given string “racecar” is a palindrome or not. It uses the // operator for floor division to determine the midpoint of the string and compare corresponding characters from the beginning and end, iterating up to the midpoint.

Q. What is the output of the above code snippet and explain the logic?

text = “hello world”

result = “”

for i in range(len(text)):

    if i % 2 == 0:

        result += text[i].upper()

    else:

        result += text[i]

print(result)

Ans. The output of the above code is HlLo WoRlD. The logic alternates between uppercasing and keeping the original case of the characters based on their position (even or odd index).