The most commonly used object in any project and in any programming language is String only. A string is a list of characters in order enclose by single quote, double quote or triple quotes. Python string is immutable modifications are not allow once it is create. String index starts from 0, it support both forward and backward indexing. Each character is encoded in the ASCII or Unicode character. Strings are use widely in many different applications, such as storing and manipulating text data, representing names, addresses, and other types of data that can be represent as text.

Creating a String in Python

We can create a string by enclosing the characters in single-quotes, double- quotes or triple-quotes. Triple-quotes is generally use for multiline string.

#single quotes  
key1 = 'Basic Engineer'
print('single quotes')
print(key1)  

#double quotes  
key2 = "Basic Engineer"  
print("double quote")
print(key2)  
  
#triple quotes  
key3 = '''Basic Engineer Basic Engineer Basic Engineer
Basic Engineer Basic Engineer Basic Engineer Basic Engineer'''
print('''triple quotes''')
print(key3)  
Example of Creating a sting in Python

Let’s run the code

Example of Creating a sting in Python
Example of Creating a sting in Python

Strings Splitting and indexing in Python

The slice operator [] is use to access the individual characters of the string and colon operator use to access the substring from the given string. The indexing of the Python strings starts from 0.

A Example of Strings Splitting and indexing in Python

#index
str = "BASIC"  
print(str[0])  
print(str[1])  
print(str[2])  
print(str[3])  
print(str[4])

#sliceing
str = "BASICENGINEER"   
print(str[0:])   
print(str[3:7])    
print(str[6:10])  
 
#sliceing
str = "BASICENGINEER"   
print(str[-1:])   
print(str[-3:-7])    
print(str[-6:-10])
Example of Strings Splitting and indexing in Python

Let’s run the code

Example of Strings Splitting and indexing in Python
Example of Strings Splitting and indexing in Python

Escape Sequence

Escape SequenceDescription
\newlineIt ignores the new line.
\\Backslash
\’Single Quotes
\\”Double Quotes
\aASCII Bell
\bASCII Backspace
\fASCII Formfeed
\nASCII Linefeed
\rASCII Carriege Return
\tASCII Horizontal Tab
\vASCII Vertical Tab
Escape Sequence

Example of Escape Sequence

print("basic engineer \basic engineer2 \basic engineer3")

print("\\basic engineer")

print('\'basic engineer')

print("\"basic engineer")

print("\a basic engineer")

print("basic \b engineer")

print("basic \f engineer")

print("basic \n engineer")

print("basic \r engineer")

print("basic \t engineer")

print("basic \v engineer")
A Example of Escape Sequence
Example of Escape Sequence in Python
Example of Escape Sequence in Python

Method of String

MethodsDescription
upper()converts the string to uppercase
lower()converts the string to lowercase
partition()returns a tuple
replace()replaces substring inside
find()returns the index of first occurrence of substring
rstrip()removes trailing characters
split()splits string from left
startswith()checks if string starts with the specified string
isnumeric()checks numeric characters
index()returns index of substring
swapcase()Inverts case for all letters in string.
min()returns the min alphabetical character from the string.
max()returns the max alphabetical character from the string.
Method of String

Example of Method of String in Python

#UPPERCASE
message = 'basic engineer'
print(message.upper())

#lowercase
message = 'BASIC ENGINEER'
print(message.lower())

# replace
text = 'basic engineer'
replaced_text = text.replace('basic', 'hello')
print(replaced_text)

# split
key = 'hello-student-basic-engineer'
print(key.split('-'))
A Example of Method of String in Python
Example of Method of String in Python
Example of Method of String in Python

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about Python Wikipedia please click here .

Stay Connected Stay Safe, Thank you

Categories: Software

Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

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