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. 

Introduction

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
print(total)

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

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

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}" 
print(full_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)

Examples 

EXO III.1

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

Answer.

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

 EXO III.2

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

Answer.

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

print(name.lower())
print(name.upper())
print(name.title())

 EXO III.3

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.

Answer.

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

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

print(name)
print(lenght)

 EXO III.4

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).

Answer.

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]

print(part)

 EXO III.5

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.

Answer.

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

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

print(word)

 EXO III.6

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.

Answer.

name = input("Enter your first name")

if len(name)<5 :
    surname = input("Enter your surname")
    name = name + surname
    print(name.upper())
else:
    print(name.lower())

 

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

1/12/2021

Developing a Web Application with Django Part 2: Templates

15/12/2021

Developing a Web Application with Django Part 3 : Models

7/1/2022