0% found this document useful (0 votes)
54 views3 pages

ITEC-425 / SENG-425: Python Programming Lab Lab 6: Strings Task 1

This document contains instructions and code snippets for 5 tasks related to string manipulation in Python. Task 1 has the user count the characters in a string without using len(). Task 2 uses len() to count characters. Task 3 extracts a float from a string after the colon. Task 4 slices strings and replaces a substring. Task 5 counts the words in a user-input string by splitting on spaces.

Uploaded by

Umm E Farwa Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views3 pages

ITEC-425 / SENG-425: Python Programming Lab Lab 6: Strings Task 1

This document contains instructions and code snippets for 5 tasks related to string manipulation in Python. Task 1 has the user count the characters in a string without using len(). Task 2 uses len() to count characters. Task 3 extracts a float from a string after the colon. Task 4 slices strings and replaces a substring. Task 5 counts the words in a user-input string by splitting on spaces.

Uploaded by

Umm E Farwa Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ITEC-425 / SENG-425: Python Programming Lab

Lab 6: Strings
Task 1
Write a program to calculate the length of a given message.

Note: In this case do this WITHOUT using the built-in len() function.

In [1]:
message = input("Enter a message: ")

count = 0

for letter in message:


count = count + 1

print("The length of message is ", count)

Enter a message: Hello, how are you doing?


The length of message is 25

Task 2
Modify the above program to use a the buil-in len() function to find the length of a
message.

In [2]:
message = input("Enter a message: ")
print("The length of the message is", len(message))

Enter a message: The weather is very nice.


The length of the message is 25

Task 3
Take the following Python code that stores a string:

str_val = 'X-DSPAM-Confidence:0.8475'

Use find() function and string slicing to extract the portion of the string after the colon
character and then use the float() function to convert the extracted string into a
floating point number. Verify by checking the type of the converted number.

In [3]:
str_val = 'X-DSPAM-Confidence:0.8475'

# first find the index position of the colon character


colon_pos = str_val.find(':')

# now slice the string starting from


# the next character after the above position until the end of the string
num_slice = str_val[colon_pos+1 :]

# check that we have the correct substring


Page 1 of 3
print(num_slice)

# now convert this substring to floating point value


float_num = float(num_slice)

# check that resulting value is of type float


print(type(float_num))

# print the resulting value


print(float_num)

0.8475
<class 'float'>
0.8475

Task 4
Consider the string give below, and the write python code to do the followig tasks:

str_val = 'the quick brown fox jumps over the lazy dog'

1. Print the slice of first 10 characters


2. Print the slice of last 10 characters
3. Replace the with a in the given string

In [4]:
str_val = 'the quick brown fox jumps over the lazy dog'

# 1. Print the slice of first 10 characters


print("The first 10 characters are:", str_val[0:10])

# 2. Print the slice of last 10 characters


print("The last 10 characters are:", str_val[-10:])

# 3. Replace 'the' with 'a'


new_string = str_val.replace('the', 'a')
print(new_string)

The first 10 characters are: the quick


The last 10 characters are: e lazy dog
a quick brown fox jumps over a lazy dog

Task 5
Write a program that asks the user to input a message and displays the number of words
in the message.

Hint: You can split the sentence on spaces.

In [6]:
message = input("Enter your message: ")

# break the message into words by using the split() function


split_message = message.split()
print(split_message)

word_count = 0

for wd in split_message:
word_count = word_count + 1

Page 2 of 3
print("Your message has", word_count, "words")

Enter your message: to be or not to be, that is the question?


['to', 'be', 'or', 'not', 'to', 'be,', 'that', 'is', 'the', 'question?']
Your message has 10 words

Page 3 of 3

You might also like