Some commonly used string functions in Python:

  1. Conversion Functions:
    • upper(): Converts all characters in the string to uppercase.
    • lower(): Converts all characters in the string to lowercase.
    • capitalize(): Capitalizes the first character of the string.
    • title(): Converts the first character of each word to uppercase.
  2. Search and Replace Functions:
    • find(substring): Returns the lowest index in the string where substring is found.
    • rfind(substring): Returns the highest index in the string where substring is found.
    • index(substring): Like find(), but raises ValueError if the substring is not found.
    • rindex(substring): Like rfind(), but raises ValueError if the substring is not found.
    • count(substring): Returns the number of occurrences of substring in the string.
    • replace(old, new): Returns a copy of the string with all occurrences of substring old replaced by new.
  3. Substring Functions:
    • startswith(prefix): Returns True if the string starts with the specified prefix, otherwise False.
    • endswith(suffix): Returns True if the string ends with the specified suffix, otherwise False.
    • strip(): Removes leading and trailing whitespace.
    • lstrip(): Removes leading whitespace.
    • rstrip(): Removes trailing whitespace.
    • split(sep): Splits the string into a list of substrings using the specified separator.
    • rsplit(sep): Splits the string from the right end.
    • partition(sep): Splits the string into three parts using the specified separator. Returns a tuple with (head, separator, tail).
    • rpartition(sep): Splits the string from the right end.
  4. String Formatting Functions:
    • format(): Formats the string.
    • join(iterable): Concatenates each element of the iterable (such as a list) to the string.
  5. String Testing Functions:
    • isalpha(): Returns True if all characters in the string are alphabetic.
    • isdigit(): Returns True if all characters in the string are digits.
    • isalnum(): Returns True if all characters in the string are alphanumeric (letters or numbers).
    • isspace(): Returns True if all characters in the string are whitespace.
  6. Miscellaneous Functions:
    • len(): Returns the length of the string.
    • ord(): Returns the Unicode code point of a character.
    • chr(): Returns the character that corresponds to the Unicode code point.

Looping over a String

Strings are objects that contain a sequence of single-character strings.

A single letter is classified as a string in Python. For example, string[0] is considered a string even though it is just a single character.

Here’s how you can do it-Loooping Over a String:

my_string = "Hello, World!"

for char in my_string:
print(char)

In Python, you can use a for loop to iterate over each character in a string. To loop over a string means to start with the first character in a string(Position 0) and iterate over each character until the end of the string( Position- Length-1).

#to get commonLetters from two string with case and duplicates ignored and the result #sorted /Or Not sorted alpabetically

def commonLetters(str1,str2):
    common = ""
    for i in str1:
        if i in str2 and i not in common:
            common += i
    return "".join(sorted(common))
def commonLettersnosort(str1,str2):
    common = ""
    for i in str1:
        if i in str2 and i not in common:
            common += i
    return "".join(common)
def commonLettersnosortnocase(str1,str2):
    common = ""
    for i in str1:
        if i.upper() in str2.upper() and i not in common:
            common += i
    return "".join(common)

Check for - 
commonLettersnosort('shyam','Ghanshyam') commonLettersnosortnocase('shyam','GhanShYam')

Head to Next


Discover more from HintsToday

Subscribe to get the latest posts sent to your email.

Pages ( 2 of 8 ): « Previous1 2 34 ... 8Next »

Discover more from HintsToday

Subscribe now to keep reading and get access to the full archive.

Continue reading