Net-informations.com
SiteMap  | About    

String Manipulation in Python

Strings are sequences of characters. There are numerous algorithms for processing strings, including for searching, sorting, comparing and transforming. Python strings are "immutable" which means they cannot be changed after they are created . To create a string, put the sequence of characters inside either single quotes, double quotes, or triple quotes and then assign it to a variable.

Access characters in a string

In order ot access characters from String, use the square brackets [] for slicing along with the index or indices to obtain your characters. Python String index starts from 0.

Python allows negative indexing for its sequences.

The index of -1 refers to the last item, -2 to the second last item and so on.

String Concatenation

Joining of two or more strings into a single one is called concatenation. Python uses "+" operator for joining one or more strings

output

Reverse a String

In Python Strings are sliceable. Slicing a string gives you a new string from one point in the string, backwards or forwards, to another point, by given increments. They take slice notation or a slice object in a subscript:

The subscript creates a slice by including a colon within the braces:

It works by doing [begin:end:step] - by leaving begin and end off and specifying a step of -1, it reverses a string.

output

String Methods

Python has several built-in methods associated with the string data type. These methods let us easily modify and manipulate strings. Built-in methods are those that are defined in the Python programming language and are readily available for us to use. Here are some of the most common string methods.

Python String len() method

String len() method return the length of the string.

output

Python String count() method

String count() method returns the number of occurrences of a substring in the given string.

output

Python String index() method

String index() method returns the index of a substring inside the given string.

end(optional) by default its equal to the length of the string.

output

Python String upper() method

String upper() convert the given string into Uppercase letters and return new string.

output

Python String lower() method

String lower() convert the given string into Lowercase letters and return new string.

output

Python String startswith() method

String startswith() method returns Boolean TRUE, if the string Starts with the specified substring otherwise, it will return False.

output

Python String endswith() method

String endswith() method returns Boolean TRUE, if the string Ends with the specified substring otherwise, it will return False.

output

Python String split() method

String split() method break up a string into smaller strings based on a delimiter or character.

output

example

output

example

output

Python returned split string as a List

output

Python String join() method

String join() is a string method which returns a string concatenated with the elements of an iterable.

output

Python String find() method

String find() return the index position of the first occurrence of a specified string. It will return -1, if the specified string is not found.

output

Python String strip() method

String strip() remove the specified characters from both Right hand side and Left hand side of a string (By default, White spaces) and returns the new string.

output

Python String rstrip() method

String rstrip() returns a copy of the string with trailing characters removed.

output

Python String lstrip() method

String lstrip() returns a copy of the string with leading characters removed.

output









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.