First we will use some common string functions in our example:
Converting a string to upper case in python
The upper function converts a string to capital letters
text="a brown quick fox jumps over the lazy dog"
text2=text.upper();
print(text)
print(text2)
Output:
a brown quick fox jumps over the lazy dog
A BROWN QUICK FOX JUMPS OVER THE LAZY DOG
Remember string functions do not change the original value but return the new version of the string. like in the above example value of text remains the same but returns the new version and saves that in text2.
Capitalize the first character of a sentence in python
The capitalize function converts the first letter of the sentence to a capital letter.
text="a brown quick fox jumps over the lazy dog"
text2=text.capitalize();
print(text)
print(text2)
Output:
a brown quick fox jumps over the lazy dog
A brown quick fox jumps over the lazy dog
Toggle or Swapcase in Python
The swapcase function converts small to capital and capital to small letters
text="a brown quick fox JUMPS OVER THE LAZY DOG"
text2=text.swapcase();
print(text)
print(text2)
Output:
a brown quick fox JUMPS OVER THE LAZY DOG
A BROWN QUICK FOX jumps over the lazy dog
Replace string in Python
The replace function replaces a string in a text to another string like dog into cat.
text="A brown quick fox jumps over the lazy dog"
newText=text.replace("dog","cat")
print(text)
print(newText)
Output:
A brown quick fox jumps over the lazy dog
A brown quick fox jumps over the lazy cat
There are some set of built-in functions that we can apply on strings in python to make our work easy.
- upper() converts a string to upper case
- lower() converts a string to lower case
- capitalize() Converts the first character to upper case
- casefold() Converts string into lower case
- center() Returns a centered string
- count() Returns the count of a specific character in a string
- encode() Encodes a string
- endswith() checks if a strings ends with specific character
- expandtabs() Sets the tab size of the string
- find() Searches the string for a specified value and returns the position of where it was found
- format() Formats specified values in a string
- index() Searches the string for a specified value and returns the position of where it was found
- isalnum() Returns True if all characters in the string are alphanumeric
- isalpha() Returns True if all characters in the string are in the alphabet
- isdecimal() Returns True if all characters in the string are decimals
- isdigit() Returns True if all characters in the string are digits
- isidentifier() Returns True if the string is an identifier
- islower() Returns True if all characters in the string are lower case
- isupper() Returns True if all characters in the string are upper case
- join() Joins the elements of an iterable to the end of the string
- replace() Returns a string where a specified value is replaced with a specified value
- split() Splits the string at the specified separator, and returns a list
- swapcase() Swaps cases, lower case becomes upper case and vice versa
Support us by sharing this post