Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

String Assignment

1. Let s1 be " Welcome " and s2 be " welcome ". Write the code for the following statements:
(a) Check whether s1 is equal to s2 and assign the result to a Boolean variable isEqual.
(b) Check whether s1 is equal to s2, ignoring case, and assign the result to a Boolean variable isEqual.
(c) Check whether s1 has the prefix AAA and assign the result to a Boolean variable b.
(d) Check whether s1 has the suffix AAA and assign the result to a Boolean variable b.
(e) Assign the length of s1 to a variable x.
(f) Assign the first character of s1 to a variable x.
(g) Create a new string s3 that combines s1 with s2.
(h) Create a substring of s1 starting from index 1.
(i) Create a substring of s1 from index 1 to index 4.
(j) Create a new string s3 that converts s1 to lowercase.
(k) Create a new string s3 that converts s1 to uppercase.
(l) Create a new string s3 that trims whitespace characters on both ends of s1.
(m) Replace e with E in s1.
(n) Assign the index of the first occurrence of character e in s1 to a variable x.
(o) Assign the index of the last occurrence of string abc in s1 to a variable x.

2. Suppose string s is an empty string; what is len(s)?

3. Write a program that prompts the user to enter a CNIC number in the format ddddd-ddddddd-d, where d is a
digit. The program displays Valid CNIC for a correct CNIC number or Invalid CNIC otherwise.

4. Some Web sites impose certain rules for passwords. Write a program that checks whether a string is a valid
password. Suppose the password rules are as follows:
¦ A password must have at least eight characters.
¦ A password must consist of only letters and digits.
¦ A password must contain at least two digits.
Write a program that prompts the user to enter a password and displays valid password if the rules are followed or
invalid password otherwise.

5. Write a program that counts and display the number of letters in a string.

6. You can check whether a string is a substring of another string by using the find method in the str class. Write
your own program to implement find. Write a program that prompts the user to enter two strings and then
checks whether the first string is a substring of the second string.

7. Write a test program that prompts the user to enter a string and then prints the reverse of it.
8.

9.

You might also like