• Interacting With Strings in Python -Part -3

    Interacting With Strings in Python -Part -III


    Slicing and Dicing Strings

    Working with ranges of characters provides some degree of flexibility, but it doesn’t provide you with the capability to actually manipulate the string content or discover anything about it. For example, you might want to

    change the characters to uppercase or determine whether the string contains all letters. Fortunately, Python has functions that help you perform tasks of this sort. Here are the most commonly used functions:

    capitalize(): Capitalizes the first letter of a string.

     center(width, fillchar=" "): Centers a string so that it fits within the number of spaces specified by width. If you supply a character for fillchar, the function uses that character. Otherwise, center() uses spaces to create a string of the desired width.

     expandtabs(tabsize=8): Expands tabs in a string by replacing the tab with the number of spaces specified by tabsize. The function defaults to 8 spaces per tab when tabsize isn’t provided.

     isalnum(): Returns True when the string has at least one character and all characters are alphanumeric (letters or numbers).

     isalpha(): Returns True when the string has at least one character and all characters are alphabetic (letters only).

     isdecimal(): Returns True when a Unicode string contains only decimal characters.

    isdigit(): Returns True when a string contains only digits (numbers and not letters).

     islower(): Returns True when a string has at least one alphabetic character and all alphabetic characters are in lowercase.

     isnumeric(): Returns True when a Unicode string contains only numeric characters.

     isspace(): Returns True when a string contains only whitespace char-acters (which includes spaces, tabs, carriage returns, linefeeds, form feeds, and vertical tabs, but not the backspace).

     istitle(): Returns True when a string is cased for use as a title, such as Hello World. However, the function requires that even little words have the title case. For example, Follow a Star returns False, even though it’s properly cased, but Follow A Star returns True.

     isupper(): Returns True when a string has at least one alphabetic character and all alphabetic characters are in uppercase.

     join(seq): Creates a string in which the base string is separated in turn by each character in seq in a repetitive fashion. For example, if you start with MyString = "Hello" and type print(MyString. join("!*!")), the output is !Hello*Hello!.

     len(string): Obtains the length of string.

     ljust(width, fillchar=" "): Left justifies a string so that it fits within the number of spaces specified by width. If you supply a charac-ter for fillchar, the function uses that character. Otherwise, ljust() uses spaces to create a string of the desired width.

     lower(): Converts all uppercase letters in a string to lowercase letters.

     lstrip(): Removes all leading whitespace characters in a string.

     max(str): Returns the character that has the maximum numeric value in str. For example, a would have a larger numeric value than A.

     min(str): Returns the character that has the minimum numeric value in str. For example, A would have a smaller numeric value than a.

     rjust(width, fillchar=" "): Right justifies a string so that it fits within the number of spaces specified by width. If you supply a charac-ter for fillchar, the function uses that character. Otherwise, rjust() uses spaces to create a string of the desired width.

     rstrip(): Removes all trailing whitespace characters in a string.

     split(str=" ", num=string.count(str)): Splits a string into sub-strings using the delimiter specified by str (when supplied). The default is to use a space as a delimiter. Consequently, if your string contains A Fine Day, the output would be three substrings consisting of A, Fine, and Day. You use num to define the number of substrings to return. The default is to return every substring that the function can produce.

     splitlines(num=string.count('\n')): Splits a string that contains newline (\n) characters into individual strings. Each break occurs at the newline character. The output has the newline characters removed. You can use num to specify the number of strings to return.

     strip(): Removes all leading and trailing whitespace characters in a string.

     swapcase(): Inverts the case for each alphabetic character in a string.

     title(): Returns a string in which the initial letter in each word is in uppercase and all remaining letters in the word are in lowercase.

     upper(): Converts all lowercase letters in a string to uppercase letters.

     zfill (width): Returns a string that is left-padded with zeros so that the resulting string is the size of width. This function is designed for use with strings containing numeric values. It retains the original sign information (if any) supplied with the number.

    Playing with these functions a bit can help you understand them better. The following steps create an example that demonstrates some of the tasks you can perform using these functions.
     Open a Python File window.

    You see an editor in which you can type the example code.

     Type the following code into the window — pressing Enter after each line:

    MyString = " Hello World "

    print(MyString.upper())

    print(MyString.strip()) print(MyString.center(21, "*")) print(MyString.strip().center(21, "*"))

    print(MyString.isdigit())
    print(MyString.istitle())

    print(max(MyString))

    print(MyString.split())
    print(MyString.split()[0])

    The code begins by creating MyString, which includes spaces before and after the text so that you can see how space-related functions work. The initial task is to convert all the characters to uppercase.


    Removing extra space is a common task in application development. The strip() function performs this task well. The center() function lets you add padding to both the left and right side of a string so that it consumes a desired amount of space. When you combine the strip() and center() functions, the output is different from when you use the center() function alone.

    You can combine functions to produce a desired result. Python executes each of the functions one at a time from left to right. The order in which the functions appear will affect the output, and developers commonly make the mistake of putting the functions in the wrong order. If your output is different from what you expected, try changing the function order.

    Some functions work on the string as an input rather than on the string instance. The max() function falls into this category. If you had typed MyString.max(), Python would have displayed an error. The bulleted list that appears earlier in this section shows which functions require this sort of string input.

    When working with functions that produce a list as an output, you can access an individual member by providing an index to it. The example shows how to use split() to split the string into substrings. It then shows how to access just the first substring in the list. You find out more about working with lists in Post 12.

     Choose RunRun Module.

    You see a Python Shell window open. The application outputs a number of modified strings.

    There are times when you need to locate specific information in a string. For example, you may want to know whether a string contains the word Hello in it. One of the essential purposes behind creating and maintaining data is to be able to search it later to locate specific bits of information. Strings are no different — they’re most useful when you can find what you need quickly and without any problems. Python provides a number of functions for searching strings. Here are the most commonly used functions:

     count(str, beg= 0, end=len(string)): Counts how many times str occurs in a string. You can limit the search by specifying a beginning index using beg or an ending index using end.

     endswith(suffix, beg=0, end=len(string)): Returns True when a string ends with the characters specified by suffix. You can limit the check by specifying a beginning index using beg or an ending index using end.

     find(str, beg=0, end=len(string)): Determines whether str occurs in a string and outputs the index of the location. You can limit the search by specifying a beginning index using beg or a ending index using end.

     index(str, beg=0, end=len(string)): Provides the same function-ality as find(), but raises an exception when str isn’t found.

     replace(old, new [, max]): Replaces all occurrences of the char-acter sequence specified by old in a string with the character sequence specified by new. You can limit the number of replacements by specifying a value for max.

     rfind(str, beg=0, end=len(string)): Provides the same function-ality as find(), but searches backward from the end of the string instead of the beginning.

     rindex(str, beg=0, end=len(string)): Provides the same function-ality as index(), but searches backward from the end of the string instead of the beginning.

     startswith(prefix, beg=0, end=len(string)): Returns True when a string begins with the characters specified by prefix. You can limit the check by specifying a beginning index using beg or an ending index using end.

    Finding the data that you need is an essential programming task — one that is required no matter what kind of application you create. The following steps help you create an example that demonstrates the use of search functionality within strings..

     Open a Python File window.

    You see an editor in which you can type the example code.

     Type the following code into the window — pressing Enter after each line:

    SearchMe = "The apple is red and the berry is blue!"

    print(SearchMe.find("is"))
    print(SearchMe.rfind("is"))

    print(SearchMe.count("is"))

    print(SearchMe.startswith("The"))
    print(SearchMe.endswith("The"))

    print(SearchMe.replace("apple", "car")
    .replace("berry", "truck"))

    The example begins by creating SearchMe, a string with two instances of the word is. The two instances are important because they demonstrate how searches differ depending on where you start. When using find(), the example starts from the beginning of the string. By contrast, rfind() starts from the end of the string.

    Of course, you won’t always know how many times a certain set of characters appears in a string. The count() function lets you determine this value.

    Depending on the kind of data you work with, sometimes the data is heavily formatted and you can use a particular pattern to your advan-tage. For example, you can determine whether a particular string (or substring) ends or begins with a specific sequence of characters. You could just as easily use this technique to look for a part number.

    The final bit of code replaces apple with car and berry with truck. Notice the technique used to place the code on two lines. In some cases, your code will need to appear on multiple lines to make it more readable.

     Choose RunRun Module.


    You see a Python Shell window open. The application displays the output. Notice especially that the searches returned different indexes based on where they started in the string. Using the correct function when performing searches is essential to ensure that you get the results you expected.
  • 0 comments:

    Post a Comment

    Powered by Blogger.

    Tags

    Popular Posts