In this training, you will learn to program with Python using practical examples. In this tutorial, you’ll learn how to use Strings variables in a python program. 


A string is a series of characters. Anything inside quotes is considered a string in Python, and you can use single or double quotes around your strings like this:

    "This is a string." 
    'This is also a string.'

Strings and Numbers as Variables

If you want to use a variable that has been defined as a string in a calculation, you will have to convert the string to a number before it can be used.

var  = int(input(“Enter a number : ”)
total = var + 15

To concatenate a string with a number you should convert previously that number to string.

age  = 25
name = “Joseph”
ID = name + str(age)

Changing Case in a String with Methods

One of the simplest tasks you can do with strings is change the case of the words in a string.

Method + Example Rule
len(word) Finds the length of the variable called word
word.upper() Changes the string into upper case
word.title() Changes a phrase so that every word has a capital letter at the beginning with the rest of the letters in the word in lower case (i.e.Title Case).
word.lower() Changes the string into lower case.
word.capitalize() the first word has a capital letter at the beginning and everything else is in lower case.
print (“Hello world”[7:10]) Each letter is assigned an index number to identify its position in the phrase, including the space. Python starts counting from 0, not 1.

A method is an action that Python can perform on a piece of data. The dot ( . ) after name in name.title() tells Python to make the title() method act on the variable name .

Using Variables in Strings

In some situations, you’ll want to use a variable’s value inside a string. For example, you might want two variables to represent a first name and a last name respectively, and then want to combine those values to display some- one’s full name:

first_name = "fred"
last_name = "Bond"
full_name = f"{first_name} {last_name}" 
first_name = "fred"
last_name = "Bond"
full_name = f"{first_name} {last_name}" print(f"Hello, {full_name.title()}!")
first_name = "fred"
last_name = "Bond"
full_name = f"{first_name} {last_name}" message = f"Hello, {full_name.title()}!" print(message)



Question. Ask the user to enter their first name and then display the length of their name.


name = input("Enter your first name : ")
lenght = len(name)
print("The lenght of your name is : ", lenght)


Question. Use a variable to represent a person’s name, and then print that person’s name in lowercase, uppercase, and title case.


name = input("Enter your first name : ")



Question. Ask the user to enter their first name and then ask them to enter their surname. Join them together with a space between and display the name and the length of whole name.


firstname = input("Enter your first name : ")
lastname = input("Enter your last name : ")

name = firstname + " " + lastname
lenght = len(name)



Question. Ask the user to type in the first line of a nursery rhyme and display the length of the string. Ask for a starting number and an ending number and then display just that section of the text (remember Python starts counting from 0 and not 1).


phrase = input("Enter the first line of a nursery rhyme : ")

length = len(phrase)
print("This phrase has ", length, " letters in it")

start = int(input("Enter a starting number : "))
end = int(input("Enter an end number : "))

part = phrase[start:end]



Question. Write a Python program to add 'ing' at the end of a given string (length should be at least 3). If the given string already ends with 'ing' then add 'ly' instead. If the string length of the given string is less than 3, leave it unchanged.


word = input("Type a word : ")
word = word.lower()
length = len(word)

if length > 2 :
    if word[-3:] == "ing":
        word = word + "ly"
        word = word + "ing"



Question. Ask the user to enter their first name. If the length of their first name is under five characters, ask them to enter their surname and join them together (without a space) and display the name in upper case. If the length of the first name is five or more characters, display their first name in lower case.


name = input("Enter your first name")

if len(name)<5 :
    surname = input("Enter your surname")
    name = name + surname


0 comment

There are no comments yet.

Log in to leave a reply

Related posts

Developing a Web Application with Django Part 1: Getting Started


Developing a Web Application with Django Part 2: Templates


Developing a Web Application with Django Part 3 : Models