Python String Methods
Strings are a fundamental data type in Python, and Python provides a rich set of methods for working with strings.
For example:
x = "Hello, World!" # Assign a string to the variable x
print(x, type(x)) # out put: python <class 'str'>
print(dir(x)) # prompt the methods of string's ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']Str.upper() Method in Python
the str.upper() method is a handy tool for text processing. It allows you to convert all characters in a string to uppercase. This can be particularly useful when you need to standardize text or perform case-insensitive comparisons.
original_string = "Hello, World!"
uppercase_string = original_string.upper()
print(uppercase_string) #HELLO, WORLD!Str.lower() Method in Python
the str.lower() method is a useful tool for text manipulation. It allows you to convert all characters in a string to lowercase.
original_string = "Hello, World!"
lowercase_string = original_string.lower()
print(lowercase_string) #hello, world!Str.title() Method in Python
the str.title() method is a valuable tool for text processing. It capitalizes the first character of each word in a string while making all other characters lowercase. This is particularly useful when you want to present text in a title or heading format.
original_string = '''Betty Botter bought some butter
But she said the butter’s bitter
If I put it in my batter, it will make my batter bitter
But a bit of better butter will make my batter better
So ‘twas better Betty Botter bought a bit of better butter'''
title_string = original_string.title()
print(title_string)Output :
Betty Botter bought some butter
But she said the butter’s bitter
If I put it in my batter, it will make my batter bitter
But a bit of better butter will make my batter better
So ‘twas better Betty Botter bought a bit of better butterStr.casefold() Method in Python
the str.casefold() method is a valuable tool for text manipulation and comparison. It is similar to str.lower(), but with additional Unicode support.
original_string = "Straße"
casefolded_string = original_string.casefold()
print(casefolded_string) #strasseStr.swapcase() Method in Python
the str.swapcase() method is a convenient tool for text manipulation. It swaps the case of each character in a string, converting uppercase characters to lowercase and vice versa.
original_string = "Python Programming is FUN!"
swapped_string = original_string.swapcase()
print(swapped_string) #pYTHON pROGRAMMING IS fun!ASCII in Python
Python provides built-in functions to work with ASCII values and characters:
Get Chandrakanth’s stories in your inbox
Join Medium for free to get updates from this writer.
ord(char): Returns the ASCII value (integer) of a character.
Here are some examples of using these functions:
# Convert a character to its ASCII value
char = 'A'
ascii_value = ord(char)
print(f"ASCII value of '{char}' is {ascii_value}") #ASCII value of 'A' is 65Today ‘s Task
why strings are Immutable and an Example
Strings in Python are immutable, which means that once a string is created, it cannot be changed or modified. This immutability has various advantages, including simplicity, safety, and efficiency.
string1 = "Hello, World!"
string1.lower() #hello, world!
print(id(x)) #140550212897824
string1.upper() #HELLO, WORLD!String Reversing
You can use string slicing to reverse a string.
original_string = "Hello, World!"
reversed_string = original_string[::-1]
print(reversed_string) #!dlroW ,olleHConcatenate these two strings
concatenate the two strings “Hello” and “World” using the + operator.
string1 = "Hello"
string2 = "World"
concatenated_string = string1 + string2
print(concatenated_string) #HelloWorldGet the alternate chars of the concatenate string in reverse order
string1 = "Hello"
string2 = "World"
concatenated_string = string1 + string2 # Concatenate the strings
reversed_string = concatenated_string[::-1] # Reverse the concatenated string
alternate_chars = reversed_string[::2] # Select characters at alternate positions
print(alternate_chars) #lrWol







