String methods in Python - Python Strings
String methods:
#capitalize-Only first character of string is capitalized
s1="example of string methods"
print (s1.capitalize()) #Output:Example of string methods
s2="EXAMPLE OF STRING METHODS"
print (s2.capitalize())#Output:Example of string methods
s1="example of strings"
print (s1.title()) #Output:Example Of Strings
#casefold- converts all character to lower case
s1="Example Of String Methods"
print (s1.casefold()) #Output:example of string methods
s2="ß-Beta"
#ß-lowercase is equivalent to ss. casefold converts it to ss. But lower doesn't do that.
print (s2.casefold()) #Output: ss-beta
print (s2.lower())#Output: ß-beta
s1="example of strings"
print (s1.swapcase()) #Output:EXAMPLE OF STRINGS
s2="EXAMPLE OF STRINGS"
print (s2.swapcase()) #Output:example of strings
s3="Example Of Strings"
print (s3.swapcase()) #Output:eXAMPLE oF sTRINGS
s1="Example Of Strings"
print (s1.lower()) #Output:example of strings
s2="EXAMPLE OF STRINGS??"
print (s2.lower())#Output:example of strings??
s3="1.example of strings?"
print (s3.lower()) #Output:1.example of strings?
s1="Example Of Strings"
print (s1.upper())#Output:EXAMPLE OF STRINGS
s2="EXAMPLE OF STRINGS??"
print (s2.upper())#Output:EXAMPLE OF STRINGS??
s3="1.example of strings?"
print (s3.upper()) #Output:1.EXAMPLE OF STRINGS?
str.encode(encoding=”encoding”,errors=”errors”)
encoding(Optional):Default encoding is “utf-8”
errors(Optional):Default errors is “strict”.Raise unicode error.
s1= "example öf strings"
print (s1) #Output:example öf strings
#Use backslash for the character that can't be encoded
print(s1.encode(encoding="ascii",errors="backslashreplace")) #Output:b'example \\xf6f strings'
#ignores the character that can't be encoded
print(s1.encode(encoding="ascii",errors="ignore"))#Output:b'example f strings'
#replace the character that can't be encoded with the text explanining the character.
print(s1.encode(encoding="ascii",errors="namereplace"))#Output:b'example \\N{LATIN SMALL LETTER O WITH DIAERESIS}f strings'
#Replace the character that can't be encoded with the question mark
print(s1.encode(encoding="ascii",errors="replace"))#Output:b'example ?f strings'
#Replace the character that can't be encoded with xml character.
print(s1.encode(encoding="ascii",errors="xmlcharrefreplace"))#Output:b'example öf strings'
#strict-Raise Unicode Error
print(s1.encode(encoding="ascii",errors="strict"))
#Output:UnicodeEncodeError: 'ascii' codec can't encode character '\xf6' in position 8: ordinal not in range(128)
#errors are not mentioned.Default is strict-Raise Unicode Error.
print(s1.encode(encoding="ascii"))
#Output:UnicodeEncodeError: 'ascii' codec can't encode character '\xf6' in position 8: ordinal not in range(128)
True
, if the string starts with specified value, otherwise returns False.
python3 python string-methods python-strings python-programming
Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc.. You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like __init__, __call__, __str__ etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).
Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it?
Today you're going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates. We gonna use Python OS remove( ) method to remove the duplicates on our drive. Well, that's simple you just call remove ( ) with a parameter of the name of the file you wanna remove done.
Guide to Python Programming Language
In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.