String Slicing and Other Functions in Python | Python Tutorials for Absolute Beginners

String Slicing in Python

  • start is the index where the slice begins (inclusive).
  • end is the index where the slice ends (exclusive).
  • Examples:

    1. Basic Slicing:

    Omitting start or end:

    When using slice notation and you don’t include start, slicing starts at the string from index 0. if not end then slicing goes to the end of a string.

    Negative Indexing:

    Use negative indices to slice from the end of the string The slice will start from the index you have given with the stop set to -1 as it is the end of the string.

    Step Argument:

    Other String Functions

    Strings in python can be manipulated in various ways with built-in functions that are already available in the programming language. Here are some commonly used ones:Here are some commonly used ones.

    len( )

    Returns the length of the string.

    lower() and upper()

    Converts the string to lowercase or uppercase.

    strip()

    Removes any leading and trailing whitespace.

    replace()

    Replaces a specified phrase with another specified phrase.

    split()

    Splits the string into a list where each word is a list item. The default separator is any whitespace.

    join()

    Joins the elements of an iterable to the end of the string.

    find()

    count()

    Example Program

    Here's a small program that uses some of the above functions:

    Summary

    String Slicing: Where the splitting of strings is done based on position or index which is usually used to extract a particular feature.
    String Functions: Some of the useful methods include len() ,lower() upper,strip ,replace ,split and join, find and count.

    Learning these basics will assist you in handling strings and dealing with them as and when needed in the Python programming language. Happy coding!

                            join our community for work this us

    Post a Comment

    Previous Post Next Post