String methods in Python

String methods in Python

String methods in Python - Python Strings

String methods:

  • str.capitalize(): Returns copy of the string with its first character capitalized and rest of the letters in lowercase.
#capitalize-Only first character of string is capitalized
    s1="example of string methods"
    print (s1.capitalize()) #Output:Example of string methods
    print (s2.capitalize())#Output:Example of string methods
  • str.title()- Returns copy of string where first character in every word is upper case.
s1="example of strings"
    print (s1.title()) #Output:Example Of Strings
  • str.casefold(): Returns casefolded copy of the string. Converts string to lower case. Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string.
#casefold- converts all character to lower case
    s1="Example Of String Methods"
    print (s1.casefold()) #Output:example of string methods

    #ß-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
  • str.swapcase():Returns copy of string with uppercase characters converted to lowercase and vice versa.
s1="example of strings"
    print (s1.swapcase()) #Output:EXAMPLE OF STRINGS

    print (s2.swapcase()) #Output:example of strings

    s3="Example Of Strings"
    print (s3.swapcase()) #Output:eXAMPLE oF sTRINGS
  • str.lower()-Returns copy of string in lowercase.Symbols and numbers are ignored.
s1="Example Of Strings"
    print (s1.lower()) #Output:example of strings

    print (s2.lower())#Output:example of strings??

    s3="1.example of strings?"
    print (s3.lower()) #Output:1.example of strings?
  • str.upper()-Returns copy of string in uppercase.Symbols and numbers are ignored.
s1="Example Of Strings"
    print (s1.upper())#Output: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():Returns an encoded version of the string in byte format.

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
    #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.
    #Output:UnicodeEncodeError: 'ascii' codec can't encode character '\xf6' in position 8: ordinal not in range(128)
  • str.startswith()- Returns True, if the string starts with specified value, otherwise returns False.

python3 python string-methods python-strings python-programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How To Compare Tesla and Ford Company By Using Magic Methods in Python

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 Programming: A Beginner’s Guide

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?

How to Remove all Duplicate Files on your Drive via Python

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

Guide to Python Programming Language

Python Tricks Every Developer Should Know

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.