Python

Constant dripping wears away a stone

Strings


Stringis one of the most popular data types in Python. Similar to other variable types, a string can be created by simply assigning a value to it.

  • substring: A substring of a string is a sequence of consecutive characters within the string.
  • prefix: A prefix of a string is a substring starting from the first character of the string.
  • suffix: A suffix of a string is a substring ending with the last character of the string.
  • Accessing Values in Strings:

    Substrings of a string can be accessed using the square brackets to slice along with indices. Unlike other programming languages, Python does not have character data type. For example, to get the first character in the stringstring1= "Hello World!",usestring1[0]. To get the substring"World",usestring1[6:11].Please note that for the substring string1[6:11], 6 is the starting index and 11 is the ending index, and 6 is inclusive while 11 is exclusive.

  • String Operators:

    String data type has its special operators. If we have two strings string1 = "Hello" and string2 = "World":

    • + (concatenation):Concatenate two strings by appending the right operand string to the end of the left operand string. For example, string1 + string2 is "HelloWorld".
    • * (repetition):Concatenate the same string multiple times. For example, string1*3 is "HelloHelloHello".
    • [] (slice):Get the character at a given index. For example, string1[2] is letter l.
    • [:] (range slice):Get the substring from a given index range excluding the ending index of the range. For example, string1[1:3] is the substring "el".
    • in (membership): Return True if the left operant is contained in the right operand (the given string); otherwise, return False. It is case sensitive. For example, the statement"He" in string1is True, while"he" in string1is False.
    • not in (membership): Return True if the left operant is not contained in the right operand (the given string); otherwise, return False.
    • r (raw string): Raw strings stay the way you write it.ris put right before the quotation marks. It can be in either lower case or upper case. For example, both print(r'\n') and print(R'\n') print \n to the screen.
    • u (Unicode string):In general, in Python, strings are stored as 8-bit ASCII characters. Unicode strings are stored as 16-bit unicode characters. Just like raw strings that use the prefixr, unicode strings use the prefixu,which is put right before the quotation marks.
  • String formatting operator %:

    The string formatting operator is used inside a string as a placeholder. The%operator is put after the string to separate it from the values that are to be inserted in the place of the placeholders.%dis for integer numbers,%fis for floating-point numbers,%bis for binary numbers, and%sis for strings, etc.

  • Built-in String Functions:
    • capitalize(): returns a new string with the first character in the given string capitalized and all other characters lowercased. The original string is unchanged.
    • casefold(): returns a new string with all characters in the given string lowercased. The original string is unchanged.
    • center(width[, fillchar]):returns a new string that is created by putting the original string in the center with its left and right sides padded with the specified characterfillcharto a total length ofwidth.Iffillcharis not given, use whitespace as the padding character. The original string is unchanged.
      • width: Required. A number that specifies the length of the string after being padded with the specified characters.
      • fillchar: Optional. A character that is used to pad the string. If it is not provided, use whitespace as the default argument.
    • count(str[, start, end]): returns the number of occurrences of the substringstrin the given string. Ifstartis given, instead of using the entire given string, use the substring from the starting indexstartto the end of the string. If bothstartandendare given, use the substring between the starting indexstartto the ending indexend.
      • str: Required. A substring whose number of occurences in a given string to be found.
      • start: Optional. A number that specifies the starting index within the given string where the search of the substringstrstarts.
      • end: Optional. A number that specifies the ending index within the given string where the search of the substringstrends.
    • encode([encoding='UTF-8', error='strict']):encodes the given string using the specified encoding schemeencodingand the error handling schemeerrors,and returns a new string in the encoded form. The original string is unchanged.
      • encoding: Optional. A string that specifies the encoding scheme to be used. This is a list of encoding schemes.
      • error: Optional. A string that specifies the error handling scheme to be used. The following is a list of error handling schemes:
    • endswith(suffix[, start, end]):returns True if the given string ends with the substringsuffix;otherwise, returns False. The same as in the methodcount(),ifstartor bothstartandendare given, instead of using the entire given string, use the substring between the starting indexstart(inclusive) and the ending indexend(exclusive) in the given string.
      • suffix: Required. A substring to be checked whether the given string ends with.
      • start: Optional. A number that specifies the starting index within the given string where the search of the substringsuffixstarts.
      • end: Optional. A number that specifies the ending index within the given string where the search of the substringsuffixends.
    • find(str[,start, end]): determines if the substringstrappears in the given string and returns its starting index in the given string if it is found; otherwise, returns -1 if it is not found. The same as in the methodcount(), ifstartor bothstartandendare given, instead of using the entire given string, use the substring between the starting indexstart(inclusive) and the ending indexend(exclusive) in the given string.
    • index(str[,start, end]):same as the functionfind(), but instead of returning -1, it raises an exception if the substringstris not found in the given string.
    • isalnum():returns True if all characters in the string are alphanumeric; otherwise, return False.
    • isalpha():returns True if all characters in the string are alphabetic; otherwise, return False.
    • isdecimal():returns True if the string contains only decimal characters; otherwise, return False.
    • isdigit():returns True if the string contains only digits 0-9; otherwise, return False.
    • isnumeric():returns True if the string contains only numeric characters; otherwise, return False.
    • islower():returns True if all characters in the string are lower case; otherwise, return False
    • isspace():returns True if the string contains only whitespace characters.
    • istitle():returns True if the first letter of each word in the string is upper case and the remaining letters are lower case; otherwise, return False.
    • isupper():returns True if all characters in the string are upper case; ohterwise, return False.
    • join(seq): joins the elements in the iterableseqwith the string.
      • seq:Required. It is an interable data type.
    • len(str): returns the length of the stringstr.
    • ljust(width[, fillchar]): returns a new string which is the left-justified version of the given string padded with the specified characterfillcharto a total length ofwidth.If fillcharis not provided, use whitespace as the default argument. The original string is unchanged.
    • lower(): returns a new string with all the characters in the given string lowercased. The original string is unchanged.
    • lstrip([str]): returns the left trimmed version of the given string by removing a longest possible prefix of the given string satisfying that all the characters in the prefix occur in the stringstr ifstris given. Returns the left trimmed version of the given string by removing all whitespaces at the beginning of the given string ifstris not given. The original string is unchanged.
      • str: Optional. A string whose characters are to be removed from the given string as prefix characters.
    • maketrans(x[, y, z]): returns a translation table that is usually used in the functiontranslate. If onlyxis provided, there needs a dictionary describing how to perform the replacement; if bothxandyare given, xandyhave to be of the same length. And each character inxis to be replaced with the corresponding character iny; ifzis also given, the characters inzare to be removed from the string.
    • partition(str):returns a tuple consisting of three substrings obtained by partitioning the given string using the first appearance of the substringstr. Ifstrdoes not occur in the given string, returns a tuple consisting of three strings with the original string as the first element and two empty strings as the last two elements. The original string is unchanged.
      • str: A substring used to partition the given string.
    • replace(old, new [, maxreplace]):replaces all occurences of the substringoldwith the substringnewin the given string ifmaxreplaceis not given and returns the new string. Ifmaxreplaceis given, replaces firstmaxreplaceoccurrences of the substringoldwith the substringnewin the given string. The original string is unchanged.
      • old: A substring to be replaced.
      • new: A substring used to replace the old substring.
      • max: A number specifying the number of occurrences of the old substring to be replaced.
    • rfind(str[, start, end]): returns -1 if the substringstrdoes not occur in the given string; otherwise, returns the index of the last occurrence of the substringstrin the given string. The same as in the methodcount(), ifstartor bothstartandendare given, instead of using the entire given string, use the substring between the starting indexstart(inclusive) and the ending indexend(exclusive) in the given string.
    • rindex(str[, start, end]): It is the same as the functionrfind(), but it raises an exception if the substringstris not found in the given string.
    • rjust(width[, fillchar]): returns a new string which is the right-justified version of the given string padded with the specified characterfillcharto a total length ofwidth.If fillcharis not provided, use whitespace as the default argument. The original string is unchanged.
    • rstrip([str]): returns the right trimmed version of the given string by removing a longest possible suffix of the given string satisfying that all the characters in the suffix occur in the stringstr, ifstris given. Returns the right trimmed version of the given string by removing all whitespaces at the end of the given string ifstris not given. The original string is unchanged.
    • split(str[, maxsplit=-1]): returns a list consisting of the substrings obtained by splitting the given string with up tomaxsplitoccurrences of the substringstr. Ifmaxsplitis not given or is the default value -1, use all occurrences of the substring.
    • splitlines([keeplinebreaks=False]): returns a list of lines in the given string obtained by splitting the given string with the line breaker. keeplinebreaksis a boolean type. Its default value is False. If it is True, the line breaker is kept in the resulted lines.
    • startwith(str[, start, end):returns True if the given string starts with the substringstr; otherwise, returns False. The same as in the methodcount(), ifstartor bothstartandendare given, instead of using the entire given string, use the substring between the starting indexstart(inclusive) and the ending indexend(exclusive) in the given string.
    • strip([str]): It is the combination of the functionslstrip()andrstrip(). It removes the beginning characters and ending characters that occur in the stringstr and returns a new string. Ifstris not given, removes all whitespaces at the beginning and the end of the given string and returns a new string. The original string is unchanged.
    • swapcase(): swaps cases in the given string such that lower case characters become upper case characters and vice versa and returns a new string. The original string is unchanged.
    • title(): returns a new string where the first character in every word in the given string is uppercased. The original string is unchanged.
    • translate(table): returns a new string where some characters are replaced with the corresponding characters described in the translation tabletable, which is a dictionary describing how to perform the translation. It is usually created by the functionmaketrans().
    • upper(): returns a new string where all characters in the given string are uppercased. The original string is unchanged.
    • zfill(width): returns a zero padded string by adding zeros at the beginning of the string to the specified length ofwidth.