before this article start, we are discussing all basic things till how variable declarations and data types, But in this article, we are discussing python strings with example. Let’s Start …

Python String 

Each and every programming language, there have one string as one data type, String means group of character. Let suppose, If you want to store your name that time they have multiple characters means at that time you have to take a String variable

Here in python, you are declaring string using two methods using single cots (‘ ‘) or using double cots (” “). Let understand with one simple example in that example I am using a print() function.

print("SMGPLAZA")
print('SMGPLAZA')
a="smgplaza"
print(a)

In this, we are using both tricks, But In the first two we are print directly and in third one, we first declare variable then store the value and then print It.

SMGPLAZA
SMGPLAZA
smgplaza

Python Multiline Strings

If you want to declare a multi-line string and You can use triple cots like (“”” “””) like below,

a = """Welcome To SMGPLAZA
Our motto is a simple solution For
a complex problem"""
print(a)

Output of above code is like.

Welcome To SMGPLAZA
Our motto is a simple solution For
a complex problem

Strings are Arrays

That means One Type Of Indexing, If You want first char of String Then You can write like StringName[0], example is given below.

a = "SMGPLAZA"
print(a[1])

We are Here declare a as String And Give The Value SMGPLAZA and print second value of SMGPLAZA is M output is below,

M

Slicing

You can return a range of characters by using the slice syntax, In these, You have To simply specify The Starting Index and Ending Index, and both are separated by a colon. Example Is Given Below,

b = "SMGPLAZA"
print(b[2:5])

Above Example We are given 2 as a starting index means they start with 3rd index because index always start with 0, An d Here Ending Index is Given At 5.

GPL	

String Length

To Find Length Of String You can Use len() Function. Example Is Given Below,

a = "SMGPLAZA"
print(len(a))

Output OF above code is 8 because that string content 8 characters.

8

String Methods

Python Has some in-built method that you can use on any string. LIst of all methods are listed below

MethodDescription
capitalize()Converts the first character to upper case
casefold()Converts string into lower case
center()Returns a centered string
count()Returns the number of times a specified value occurs in a string
encode()Returns an encoded version of the string
endswith()Returns true if the string ends with the specified value
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
format_map()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
isnumeric()Returns True if all characters in the string are numeric
isprintable()Returns True if all characters in the string are printable
isspace()Returns True if all characters in the string are whitespaces
istitle()Returns True if the string follows the rules of a title
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
ljust()Returns a left justified version of the string
lower()Converts a string into lower case
lstrip()Returns a left trim version of the string
maketrans()Returns a translation table to be used in translations
partition()Returns a tuple where the string is parted into three parts
replace()Returns a string where a specified value is replaced with a specified value
rfind()Searches the string for a specified value and returns the last position of where it was found
rindex()Searches the string for a specified value and returns the last position of where it was found
rjust()Returns a right justified version of the string
rpartition()Returns a tuple where the string is parted into three parts
rsplit()Splits the string at the specified separator, and returns a list
rstrip()Returns a right trim version of the string
split()Splits the string at the specified separator, and returns a list
splitlines()Splits the string at line breaks and returns a list
startswith()Returns true if the string starts with the specified value
strip()Returns a trimmed version of the string
swapcase()Swaps cases, lower case becomes upper case and vice versa
title()Converts the first character of each word to upper case
translate()Returns a translated string
upper()Converts a string into upper case

Also visit this articles :-

For All python article, you have to follow this link, https://smgplaza.com/category/python/

If you have any doubts any suggestions or any query DM us one below Social media handles.

Our Social Media Handles:-

Instagram Handle :- https://www.instagram.com/smgplaza/

Twitter Handle :- https://twitter.com/smgplaza

Thank you so much for your support, you all are very valuable for us. You have to follow us using your mail id and also follow us on social media for more updates. We are one by one covering all topics of python and android both. If you have any query then comment me on the below comment box.