String Data Type
A string is an ordered set of characters represented either in single or double quotes. A string can contain alphabets, digits and special characters.
Python doesn’t support character data type instead a single character is considered as a string of length 1.
The indexing of a string with length n starts from 0 to n-1.
An escape or non-printable character is represented with backslash (\) in strings.
String handling in python is a simple and easy task as we have various built-in functions and operators.
In python, the operator + (plus sign) is a concatenation operator and the operator * (asterisk) is the repetition operator.
We can print the subset of a string by using the slice operator ( [ ] or [ : ] ) with index starting at 0.
str1 = “Tutorials” str2 = “Duniya” print str1 # Prints complete string print str1 # Prints 5th character of the string print str1[2:7] # Prints characters starting from 2nd to 7th print str1[4: ] # Prints complete string starting from 4th character print str2 * 3 # Prints string 2 times print str1 + str2 # Prints concatenation of str1 and str2
Tutorials i torial rials DuniyaDuniya TutorialsDuniya
We can update the whole string but can’t update a particular character within a string.
myStr = “Tutorials” myStr = “A” # An error will occur as a particular character can’t be updated myStr = “Duniya” # whole string can be updated print(myStr) # prints Duniya
Python built-in string functions
|capitalize()||Returns a copy of the string only with its first character capitalized.|
|count(string,begin,end)||Returns the number of occurrences of a substring in a string.|
|endswith(suffix, beg=0, end=len(string)||Returns a boolean value if the string terminates with given suffix between begin and end.|
|expandtabs(tabsize=8)||Returns a copy of the string in which tab characters i.e. '\t' are expanded using spaces, optionally using the given tabsize (default 8).|
|find(substring, beginIndex, endIndex)||Returns the index value of the string where substring is found between begin index and end index.|
|format(value)||Returns a formatted version of S using the passed value.|
|isalnum()||Returns true if string has at least 1 character and all characters are alphanumeric and false otherwise.|
|isalpha()||Returns true if string has at least 1 character and all characters are alphabetic and false otherwise.|
|isdigit()||Returns true if string contains only digits and false otherwise.|
|islower()||Returns true if string has at least 1 character and all characters are in lowercase and false otherwise.|
|isupper()||Returns true if string has at least 1 character and all characters are in uppercase and false otherwise.|
|isnumeric()||Returns true if a string contains only numeric characters and false otherwise.|
|isdecimal()||Returns true if all the characters of the string are decimals otherwise false.|
|isspace()||Returns true if string contains only whitespace characters and false otherwise.|
|istitle()||Returns true if the string is titled properly and false otherwise. A title string is the one in which the first character is uppercase and other characters are lowercase.|
|isidentifier()||Returns true if the string is a valid identifier otherwise false.|
|join(seq)||Returns a string which is the concatenation of the strings in the sequence seq.|
|len(string)||Returns the length of the string.|
|lower()||Returns a copy of the string in which all characters have been lowercased.|
|upper()||Returns a copy of the string in which all characters have been uppercased.|
|lstrip()||Returns a copy of the string in which all chars have been stripped from the beginning of the string.|
|replace(old, new [,count])||Returns a copy of the string in which the occurrences of old have been replaced with new, optionally restricting the number of replacements to count.|
|rfind(str, beg=0,end=len(string))||Same as find(), but search backwards in string.|
|rindex( str, beg=0, end=len(string))||Same as index(), but search backwards in string.|
|rjust(width,[, fillchar])||Returns a space-padded string with the original string right-justified to a total of width columns.|
|rstrip()||Returns a copy of the string in which all chars have been stripped from the end of the string (default whitespace characters).|
|split(str="", num=string.count(str))||Returns a list of all the words in the string, using str as the separator (splits on all whitespace if left unspecified), optionally limiting the number of splits to num.|
|startswith(str, beg=0,end=len(string))||Returns true if string starts with str otherwise false.|
|splitlines( num=string.count('\n'))||Returns a list with all the lines in string, optionally including the line breaks, if num is supplied.|
|strip([chars])||Returns a copy of the string in which all chars have been stripped from the beginning and the end of the string.|
|swapcase()||Returns a copy of the string in which all the case-based characters have had their case swapped.|
|title()||Returns a copy of the string in which first characters of all the words are capitalized.|
|translate(table, deletechars=””)||Returns a copy of the string in which all characters have been translated using table, optionally deleting all characters found in the string deletechars.|
|zfill(width)||Returns original string leftpadded with zeros to a total of width characters.|