py
py
2] Strings
1 Python Strings
Concept: Python provides several ways to access the individual characters in a string. Strings
also have methods that allow you to perform operations on them.
Little more about strings:
• A string is a sequence (a positionally ordered collection) of other objects or elements.
• A string maintains a left-to-right order among the contained items.
• Items in a string are stored and fetched by their relative position.
• Stings are immutable in Python – they cannot be changed in place after they are created. In
simple words, immutable objects can never be overwritten, e.g. we can’t change a string by
assigning to one of its positions, but we can always build a new string and assign it to the
same name.
GIFT University
statement
1
etc.
G I F T U n i v e r s i t y
2.2 Indexing
Another way that you can access the individual characters in a string is with an index. Each
character in a string has an index that specifies its position in the string. Indexing starts at 0, so
the index of the first character is 0, the index of the second character is 1, and so forth.
You can use an index to retrieve a copy of an individual character in a string.
2
3 String Concatenation
A common operation that performed on strings is concatenation, or appending one string to the
end of another string.The + operator produces a string that is the combination of the two strings
used as its operands.
Ali Babar
The last statement in this code will raise an exception because it attempts to change the value of
the first character in the string ‘Bill’.
5 String Slicing
CONCEPT: You can use slicing expressions to select a range of characters from a string
A slice is a span of items that are taken from a sequence. When you take a slice from a string, you
get a span of characters from within the string. String slices are also called substrings.
To get a slice of a string, you write an expression in the following general format:
string[start : end]
3
[8]: full_name = 'Patty Lynn Smith'
middle_name = full_name[6:10]
print(middle_name)
Lynn
If you leave out the start index in a slicing expression, Python uses 0 as the starting index. Here
is an example:
Patty
If you leave out the end index in a slicing expression, Python uses the length of the string as the
end index. Here is an example:
Smith
What do you think the following code will assign to the my_string variable?
ACEGIKMOQSUWY
4
6 Testing, Searching, and Manipulating Strings
6.1 Testing Strings with in and not in
In Python you can use the in operator to determine whether one string is contained in another
string.
string1 in string2
string1 and string2 can be either string literals or variables referencing strings. The expression
returns true if string1 is found in string2. For example, look at the following code:
5
6.4 Modification Methods
Although strings are immutable, meaning they cannot be modified, they do have a number of
methods that return modified versions of themselves.
wwwww
8 Splitting a String
Strings in Python have a method named split that returns a list containing the words in the string.
If you want to break out the month, day, and year as items in a list, you can call the split method
using the ‘/’ character as a separator, as shown here:
6
9 Exercise
9.1 Sum of Digits in a String
Write a program that asks the user to enter a series of single-digit numbers with nothing separating
them. The program should display the sum of all the single digit numbers in the string. For
example, if the user enters 2514, the method should return 12, which is the sum of 2, 5, 1, and 4.
Letters Number
A, B, and C 2
D, E, and F 3
G, H, and I 4
J, K, and L 5
M, N, and O 6
P, Q, R, and S 7
T, U, and V 8
W, X, Y, and Z 9
Write a program that asks the user to enter a 10-character telephone number in the format XXX-
XXX-XXXX. The application should display the telephone number with any alphabetic characters
that appeared in the original translated to their numeric equivalent. For example, if the user enters
555-GET-FOOD the application should display 555-438-3663.
7
That particular software house sets the criteria of password as: A password is valid if and only
if it has: * At least one lower case letter * At least one upper case letter * At least one special
characters from the above given image. * At least one digit
All other passwords are invalid. Use the functions described above to create this program.Summing
up the program requirements:
1. takes input as password from user
2. Apply the described tests
3. And prints whether the password is valid or invalid.