CodeChit

Programming, Django and Frameworks

Menu
  • Programming
  • Python
  • Django
  • Blogging
    • wordpress
    • Blogger
  • DMCA/PRIVACY
  • Videos
Menu
python string methods

Python Basic: String Methods and Operations (Basic Guide)

Posted on November 30, 2020December 10, 2020 by PK Karn

Hey coders, today we are going to see basic string manipulation and methods in python programming language.

Table Of Contents show
1 What is String?
1.1 Multiline Strings:
1.2 Strings as Array & Slicing:
1.3 Looping through string:
1.4 To calculate String length:
1.5 To Check characters or phrase in a string:
2 String Methods
2.1 capitalize(): To convert first char to uppercase:
2.2 casefold(): To convert entire string to small character:
2.3 find() and index(): To find index position value:
2.4 format(): to insert a value of a variable inside string:
2.5 You may also like

What is String?

In python, a string is a sequence of characters, where they are surrounded by double quotes(” “, or ‘ ‘). Anything between these quotes will be considered as a string, e.g: “My name is pk karn“.

Now there are lot things that you can do with these strings, e.g. string manipulation, operations. And there are also some inbuilt method to perform specific operation on string.

In python, strings are also considered as an Arrays, thus you can access characters at specific position using index value.

python string methods

Now let’s know more about string.

Multiline Strings:

To write multiline strings, you can use triple quotes like:

a = """I'm PK Karn,
and I love coding and blogging,
that's why I'm sharing this knowledge with you."""

Strings as Array & Slicing:

In python, strings are like an array, thus you can access any characters from any position. For example, if you want to access the character of 1st position then you need to put index 0 inside []

a = "Python is programming language"
print(a[0])

#Output: P
#------------------
a = "Python is programming language"
print(a[6:])   # It will give you output from position of 6th character.

#Output: is programming language

a = "Python is programming language"
print(a[10:22]) # To access element between specific range

#output: programming

Looping through string:

You can also loop through a string using for loop, with help of this you can iterate each character of string e.g:

for x in 'programmer':
  print(x)

:Output
p
r
o
g
r
a
m
m
e
r

To calculate String length:

You can use len() function to calculate length of strings, e.g:

name = "pkkarn"

print(len(name))

#output: 6

To Check characters or phrase in a string:

You can check whether a specific character or phrase exists in a string or not, by using in keyword. If it would exist then it will return a boolean result e.g. True

name = "pk karn"

if "pk" in name:
    print("It exists")

# output: It exists

String Methods

Now, let’s see some string methods that you can use to perform some specific action on a string. I’m gonna show you some important string methods that you will be using more often.

capitalize(): To convert first char to uppercase:

capitalize() method converts first character to uppercase e.g “My name is pk karn”

name = "my name is pk karn"

print(name.capitalize())

# Outpup: My name is pk karn

casefold(): To convert entire string to small character:

name = "PROGRAMMER"

print(name.casefold())

# Outpup: programmer

find() and index(): To find index position value:

we can use find() and index() both to find position of specified value from string.

text = "my name is pk karn."

x = text.find("name")

print(x)

format(): to insert a value of a variable inside string:

You can use fromat() method to insert value of variable inside a particular string, :

formatMoney = "$ {price}"
print(formatMoney.format(price = 200))

#output :$ 200

These are the basic of string in pyhton, you can read lot more about python string from their official docs.

Read also: Exception Handling in Python(Guide)

I think you liked this article, if yes then please don’t forget to share with other programmers. You can also subscribe to our blog to get such kinds of articles from us.

Thanks to visit…

You may also like

  • Django + Github Auto deployment to Heroku
    Django
    Django + Github Auto deployment to Heroku
    Hello coders, In this article, I'll show you How to deploy your Django web app t...
  • Authentication with django-allauth in Django Apps
    Django
    Authentication with django-allauth in Django Apps
    Hi coders, In this article, I will show you how to setup Authentication with hel...
  • VueJs 3 Basic Concepts – Getting started with Vue (GUIDE)
    Framework
    VueJs 3 Basic Concepts – Getting started with Vue (GUIDE)
    Hi coders, It's a brief VueJS 3 Guide for beginners. And I'm going to explain ba...
  • Arrow Function in JavaScript ES6 (GUIDE)
    JavaScript
    Arrow Function in JavaScript ES6 (GUIDE)
    Arrow function in Javascript is one of the most useful feature that was introduc...
  • Filter() method in Python
    Filter() method in Python
    The Filter() method is one of the most useful and inbuilt method in Python progr...
  • PostgreSQL basic: Create a table and Insert Data
    Database
    PostgreSQL basic: Create a table and Insert Data
    PostgreSQL is one of the best databases out there and today we're come up with t...

1 thought on “Python Basic: String Methods and Operations (Basic Guide)”

  1. Pingback: Python Basic: Lists methods and operations (Basic Guide) - CodeChit

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to create and register the component in VueJS 3
  • How to Install And Use VueX in VueJS 3 (GUIDE)
  • How to setup Vue Router in VueJS 3 (GUIDE)
©2021 CodeChit | Design: Newspaperly WordPress Theme