Hello Reader ;-; This Post might help you to improve your knowledge in strings and other string functions. ;-;
Print function, you know i guess
print("Hello World!")
Terminal:
Hello World!
Multiple Parameters
print("Hello", "World!")
Terminal:
Hello World!
Addition of String
print("Hello" + "World!")
Terminal:
HelloWorld!
Immutable String
print("""The
quick
brown
fox
jumps
over
the
lazy
dog""")
Terminal:
The
quick
brown
fox
jumps
over
the
lazy
dog
Variables
Variables stores Strings, Numbers and Bool(True, False)
hello = "Hello World!"
print(hello)
Terminal:
Hello World!
Another simpul example, lol oops
var1 = "Hello"
var2 = "World!"
print(var1 + var2)
Terminal:
HelloWorld!
Functions
Built-in String Functions
Capitalize
Converts First Letter of sentence into uppercase
str1 = "hello World"
print(str1.capitalize())
Terminal:
Hello world
UpperCase
To check if string is uppercase or not:
str1 = "hello World"
print(str1.isupper())
Terminal:
False
It is Returning False cause Every letter needs to be in uppercase
To convert them into uppercase
str1 = "hello World"
print(str1.upper())
Terminal:
HELLO WORLD
Title
First letter of every word is converted to uppercase
str1 = "hello world"
print(str1.title())
Terminal:
Hello World
Lowercase, Casefold
Both functions are similar but Casefold is a bit aggresive than Lowercase
Lowercase
str1 = "hello World"
print(str1.islower())
Terminal:
False
Every letter needs to be in lowercase
Convert to lowercase
str1 = "hello World"
print(str1.lower())
Terminal:
hello world
Casefold
Now Casefold works same as lowercase but it is a bit strict
print("Hello World".casefold())
print("ß".casefold())
print("ß".lower())
Terminal:
hello world
ss
ß
In first line as you can see it is converting string into lowercase
In second one it was supposed to convert letters into lowercase but German letter ß was already in lowercase so it converted it into 'ss'
In Third Print Statement lowercase() does nothing cause it was already in lowercase.
Count
Counts a Character in string
str1 = "hello World"
print(str1.count("l"))
Terminal:
3
Index, Find
Index and Find Fetches Index of Parameter submitted in.
Index
print("We are learning python3".index("a"))
print("We are learning python3".index("x"))
Terminal:
3
ValueError: substring not found
for second line, It is returning error cause 'x' is not present in the string but when we try same with .find() it won't throw error
Find
Find is a bit better than .index() cause it does not throw error when substring is not available
print("We are learning python3".find("a"))
print("We are learning python3".find("x"))
Terminal:
3
-1
It's returning -1 cause 'x' is not present in the string.
Text Alignment
Some Functions to edit and Align strings.
rjust
rjust justifies content on right side Its Parameter is .rjust(width, fillchar)
Here width is counted as addition of len(string) and int we entered in.
If fillchar is not entered it will be " " by default.
print("Py".rjust(5))
print("Py".rjust(5, "%"))
Terminal:
Py
%%%Py
As you can see i guess There 3 spaces before Py cause Total width is set to 5 hence 3+2("Py")=5 so there are 3 spaces instead of 5
ljust
Same as rjust, Justifies content on left side
print("Py".ljust(5))
print("Py".ljust(5, "%"))
Terminal:
Py
Py%%%
In first Py there are 3 Spaces after Py
Center
Centers Text Between fillchar
If we keep fillchar it will keep equivalent on both sides if equivalent is not possible it will prioritize left side first
print("Py".center(4))
print("Py".center(3, "%"))
print("Py".center(4, "%"))
Terminal:
Py
%Py
%Py%
In first Py there are 2 space before and after 'Py'
Split
string.split(separator)
To convert string into array
str1 = "2 is greater than 1"
array = str1.split()
print(array)
Terminal:
['2', 'is', 'greater', 'than', '1']
Separator was " " by default. Another Example with separator "-"
str1 = "2 is-greater-than 1"
print(str1.split("-"))
Terminal:
['2 is', 'greater', 'than 1']
w3Schools Python String Functions :)
Thanks :9