Saturday, September 29, 2018

Strings revisited

We know that a String is a sequence of characters. Strings in Python are immutable. You can’t change a string in place, but you can copy parts of strings to another string to get the same effect. You’ll
see how to do this later in this post.

Python string is created by enclosing characters in either single quotes or double quotes, as demonstrated in the following:

>>> 'Python is fun'
'Python is fun'

>>> "Python is easy"
'Python is easy'

The interactive interpreter echoes strings with a single quote, but all are treated exactly the same by Python.

You must be wondering why have two kinds of quote characters?

Try this program 'Strings_demo.py' and see the ouput, you might figure out the reason behind using two kinds of quotes.



In case you couldn't notice the reason is so that you can create strings containing quote characters. You can have single quotes inside double-quoted strings, or double quotes inside single-quoted strings. The output for Strings_demo.py would further clarify

 

Even three single quotes (''') or three double quotes (""") can also be used. Their most common use is
to create multiline strings. Try this on your editor and see the output-

note = '''You should know that,
... Python is a snake;
... But this programming language,
... has nothing to do with the snake as"
... it is named after Monty Python.'''

print (note)




try this -

note = 'You should know that,
... Python is a snake;
... But this programming language,
... has nothing to do with the snake as"
... it is named after Monty Python.'

print (note)


Now you know the utility of triple quotes. The print() strips quotes from strings and prints their contents. It’s meant for human output. It adds a space between each of the things it prints, and a newline at the end.

If you wish you can create an empty string as well. The following program demonstrates it's usage-







See the output: Total users: 150 

So want to build a string from other strings, and you need to start with a blank string. Also you may have noticed that you can convert other Python data types to strings by using the str() function, as we did in our previous program (message += str(users)).

Escape sequences

By preceding a character with a backslash (\), you give it a special meaning.

1. The most common escape sequence is \n, which means to begin a new line. With this you can create multi-line strings from a one-line string.

Example:  print("Languages:\nPython\nC\nJava")


 2. escape sequence \t (tab) used to align text

Example:










3. You might also need \' or \" to specify a literal single or double quote inside a string that’s quoted by the same character





You can also combine tabs and newlines in a single string. The string "\n\t" tells Python to move to a new line, and start the next line with a tab. The following example shows how you can use a one-line string to generate four lines of output:





4. Stripping Whitespace

Extra whitespace can be confusing in your programs. To programmers 'message' and 'message ' look pretty much the same. But to a program, they are two different strings. Python detects the extra space in 'message ' and considers it significant unless you tell it otherwise.

It’s important to think about whitespace, because often you’ll want to compare two strings to determine whether they are the same. For example, one important instance might involve checking people’s usernames when they log in to a website. Extra whitespace can be confusing in much simpler situations as well. Fortunately, Python makes it easy to eliminate extraneous whitespace from data that people enter.

Python can look for extra whitespace on the right and left sides of a string. To ensure that no whitespace exists at the right end of a string, use the rstrip() method.



The value stored in favorite_language contains extra whitespace at the end of the string. When you ask Python for this value in a terminal session, you can see the space at the end of the value. When the rstrip() method acts on the variable favorite_language, this extra space is removed. However, it is only removed temporarily. If you ask for the value of favorite_language again, you can see that the string looks the same as when it was entered, including the extra whitespace.


To remove the whitespace from the string permanently, you have to store the stripped value back into the variable:



 To remove the whitespace from the string, you strip the whitespace from the right side of the string and then store that value back in the original variable. The following code does this:

 favorite_language = favorite_language.rstrip()

Changing a variable’s value and then storing the new value back in the original variable is done often in programming. This is how a variable’s value can change as a program is executed or in response to user input.

We can also strip whitespace from the left side of a string using the lstrip() method or strip whitespace from both sides at once using strip().




As you may have noticed we start with a value that has whitespace at the beginning and the end.
Using rstrip() remove the extra space from the right side then remove the extra space from the left side using lstrip() and finally using strip() remove the extra spaces from both the sides.

5. Combining or Concatenating Strings

We can combine literal strings or string variables in Python by using the + operator. For example, you might want to store a first name and a last name in separate variables, and then combine them when you want to display someone’s full name:

first_name = "veevaeck"
last_name = "swami"
full_name = first_name + " " + last_name
print(full_name)


Python uses the plus symbol (+) to combine strings. In this example,
we use + to create a full name by combining a first_name, a space, and a
last_name u, giving this result:

veevaeck swami

 

 This method of combining strings is called concatenation. You can use concatenation to compose complete messages using the information you’ve stored in a variable. See the program below:



6. Changing Case in a String

a. title() - The title() function doesn’t need any additional information, so its parentheses are empty. title() displays each word in titlecase, where each word begins with a capital letter. This is useful because you’ll often want to think of a name as a piece of information.

For example, you might want your program to recognize the input values Veevaeck, VEEVAECK, and veevaeck as the same name, and display all of them as Veevaeck.




We can change a string to all uppercase or all lowercase letters as shown in the following program-



 The lower() method is particularly useful for storing data. Some times you won’t want to trust the capitalization that your users provide, so you’ll convert strings to lowercase before storing them. Then when you want to display the information, you’ll use the case that makes the most sense for
each string.

There are a few other methods which are demonstrated by the following program-



 
 The replace() is used for simple substring substitution. You give it the old substring, the
new one, and how many instances of the old substring to replace. If you omit this final
count argument, it replaces all instances. See the program below for example -



Here I'll conclude this post on Strings. Make sure you play with functions, make some more programs and try new stuff!




7. String duplication

We use the * operator to duplicate a string. See the program below:






8. Character extraction from a string

Using [] character extraction from a string is possible. To get a single character from a string, specify its offset inside square brackets after the string’s name. The first (leftmost) offset is 0, the next is 1, and so on. The last (rightmost) offset can be specified with –1 so you don’t have to count; going to the left are –2, –3, and so on.

See the program below-





What if wanna change veevaeck to beevaeck, possible?

full_name = "veevaeck"
full_name [0]= 'b'
print(full_name)


So if I try this what should be the output - beevaeck? wrong!

As we know strings are immutable, wecan’t insert a character directly into one or change the character at a specific index. so if I try full_name [0]= 'b' the program will give error.





Then what should be done if wanna change veevaeck to beevaeck?

We need to use some combination of string functions such as replace() or a slice

full_name = 'veevaeck'
full_name.replace('v', 'b')

the output is beebaeck, not beevaeck since 'v' occurs twice in full_name it was replaced by 'b' at both the locations. See the output below-


 Hence replace() doen't always give desired results. This slice is a better option.

full_name = 'veevaeck'

full_name = 'b' + full_name[1:]

print(full_name)



We can extract a substring (a part of a string) from a string by using a slice. You define a slice by using square brackets, a start offset, an end offset, and an optional step size.

Some of these can be omitted. The slice will include characters from offset start to one before end.
• [:] extracts the entire sequence from start to end.
• [ start :] specifies from the start offset to the end.
• [: end ] specifies from the beginning to the end offset minus 1.
• [ start : end ] indicates from the start offset to the end offset minus 1.
• [ start : end : step ] extracts from the start offset to the end offset minus 1, skipping characters by step.

As before, offsets go 0, 1, and so on from the start to the right, and –1,–2, and so forth from the end to the left. If you don’t specify start, the slice uses 0 (the beginning). If you don’t specify end, it uses the end of the string.

Let's see some examples-

alphabets =  'abcdefghijklmnopqrstuvwxyz'

To get the entire string we can use alphabets [:]
To get characters from 15 to the end: alphabets [15:]
To get characters from somewhere in the middle, say 12 to 14: alphabets [12:15]
 To get last three characters: alphabets [-3:]



 If you want a step size other than 1, specify it after a second colon, as shown in the next series of examples.

From the start to the end, in steps of 7 characters: alphabets [::7]
From offset 4 to 19, by 3: alphabets [4:20:3]
From offset 19 to the end, by 4: alphabets [19::4] 

See the ouput below -

 

Now guess the output for alphabets [-1::-1] and alphabets [::-1]


9. Some builtin functions

a. The len() function - it counts characters in a string. Example len(alphabets)
b. The split() function - can be used to break a string into a list of smaller strings based on some       separator. Example alphabets.split(',')


c. The join() - it collapses a list of strings into a single string.




There are some other in-built functions which are self explanatory and the next program will introduce and explain their functionality. See the program below-





The rfind(word) - gives the offset of the last word
The isalnum() - states if all of the characters in the message either letters or numbers?







Share: