Python Strings


Python strings are sequence of characters (unlike integers, floats, and booleans) enclosed in single or double quotes. The expression in brackets is called an index. The index indicates which character in the sequence you want to access. ```python #string index city = "Pune" print(city[0]) ``` Strings are immutable. following wont work `greeting[0] = 'J' `s #### looping Many a times we need to parse string or take some action on individual string characters. This can be done using while and for loop. Using while loop ```python i = 0 while i < len(city): print(city[i]) i =i + 1 ``` Using for loop ```python for letter in city: print(letter) ``` #### string slices The operator [n:m] returns the part of the string from the “n-eth” character to the “m-eth” character, including the first but excluding the last. 1. If you omit the first index (before the colon), the slice starts at the beginning of the string. 1. If you omit the second index, the slice goes to the end of the string: 2. If the first index is greater than or equal to the second the result is an empty string, represented by two quotation marks 3. You can use -ve value to start counting in reverse way as well. ```python city = "Mumbai" print(city[1:4]) print(city[:4]) print(city[3:4]) print(city[3:]) print(city[:-2]) print(city[-4:-1]) ``` #### in operator This is used to check if any character or sequnce of character exist in a string. ```python 'a' in 'banana' ``` #### string methods Strings provide methods that perform a variety of useful operations. A method is similar to a function—it takes arguments and returns a value—but the syntax is different. ```python input_statement = "Pune has a rich historical and cultural heritage. Pune is known as the 'Oxford of the East'." print('input_statement as is :', input_statement) print('input_statement.upper() :', input_statement.upper()) print('input_statement.lower() :', input_statement.lower()) print('input_statement.swapcase() :', input_statement.swapcase()) print('input_statement.encode() :', input_statement.encode(encoding='utf-8', errors='strict')) print('input_statement.title() :', input_statement.title()) print('input_statement.split() :', input_statement.split()) print("input_statement.split('a') :", input_statement.split('a')) ``` ```terminal input_statement as is : Pune has a rich historical and cultural heritage. Pune is known as the 'Oxford of the East'. input_statement.upper() : PUNE HAS A RICH HISTORICAL AND CULTURAL HERITAGE. PUNE IS KNOWN AS THE 'OXFORD OF THE EAST'. input_statement.lower() : pune has a rich historical and cultural heritage. pune is known as the 'oxford of the east'. input_statement.swapcase() : pUNE HAS A RICH HISTORICAL AND CULTURAL HERITAGE. pUNE IS KNOWN AS THE 'oXFORD OF THE eAST'. input_statement.encode() : b"Pune has a rich historical and cultural heritage. Pune is known as the 'Oxford of the East'." input_statement.title() : Pune Has A Rich Historical And Cultural Heritage. Pune Is Known As The 'Oxford Of The East'. input_statement.split() : ['Pune', 'has', 'a', 'rich', 'historical', 'and', 'cultural', 'heritage.', 'Pune', 'is', 'known', 'as', 'the', "'Oxford", 'of', 'the', "East'."] input_statement.split('a') : ['Pune h', 's ', ' rich historic', 'l ', 'nd cultur', 'l herit', 'ge. Pune is known ', "s the 'Oxford of the E", "st'."] ``` ### How to check if character in string is numeric. If a integer is store as string, this can be detected using following functions ```python temp = '2' temp.isdigit() temp.isnumeric() temp.isdecimal() ``` There is subtle difference between these three function and details can be ound [here](https://stackoverflow.com/questions/44891070/whats-the-difference-between-str-isdigit-isnumeric-and-isdecimal-in-pyth)

This post is written by Pravin

Tags :

Share it :      

Leave a comment

captcha


Tags

Elsewhere

  1. GitHub
Ad below