×

Removing the First Character from the String in Python

String

The string is the collection of characters. The strings in python are “immutable”; we cannot change a string once they are created. In python, whatever data we take as input will be read as a string. In python, string concatenation is possible.

Syntax:

my_string=input(“Data”)

Example:

my_string=input(“Python”)
print(my_string)

Output:

Python

Indexing a String

The indexing of a string in python starts with zero and counts until the last character. For accessing an element in a string, we use the method of indexing.

Syntax:

my_string= “…Data…”
my_string[n]
#where n is an index of the element in the string.

Example:

my_string= “Python”
#To access the first element in the string
print(my_string[0])
#To access the second element in the string
print(my_string[1])

Output:

p

y

Slicing a String

In python, a string can be sliced into many parts by the indexing method. The slicing method does not affect the original string but creates a string with given indexing values.

Syntax:

my_string= “…Data….”
print( my_string[n:m] )
#where n and m are any two integers

Example:

my_string= “Python”
#Starts 2ndindex to 4th index.
print(my_string[2:5])
#Starts 0th index to 3rd index.	
print(my_string[0:4])
#Starts 0th  index to 4th index.
Print(my_string[:5])

Output:

tho

pyth

Python

We can also use the negative indexes of the elements in the string to perform the slicing operation.

Example:            

my_string= “String”
#printing the last but one two elements of the string
print(my_string[-3:-1])
#printing the first two elements of the string
Print(my_string[-6:-4])

Output:

in

st

We can also reverse a string using the negative indexing method through the slicing operation.

Example:

my_string= “String”
print(my_string[::-1])

Output:

gnirts

Length of a String

The Length of the string is generally calculated using the “ len( ) ” function. The index of the last element can be calculated by using the Length of the string.

index of last element= Length of the string – 1

Example:

my_string= “String”
#printing the Length of string
print(len(my_string))

Output:

6

Deletion of an Element from the String

The strings are generally immutable, so we cannot change the given string. Hence we need to assign the updated string to a new variable. Generally, we use two functions to perform this operation they are,

  • Using replace( )
  • Using translate( )

Using replace( ):

The replace function removes a character from the string, replaces it with the given character, and updates the given string. The replace ( ) function replaces the previous character with the new character using the double quotes ( " " ).

Syntax:

string.replace( previous character, new character, count)

Parameters:

String: It is a positional argument consisting of the string.

Previous character: The character who is to be replaced.

New character: The character which is given to update the string

Count: This is an optional character which tells us how many times the given character needs to be repeated.

Example:

my_string= “ replace “
updated_string=string.replace( “l” , “u”)
#display the updated version of the string
print(updated_string)

Output:

replace

The major drawback of this method is that it replaces all the given characters with the new character, that is, if a character "j" has occurred two times in the 3rd and 6th positions of a string.

If we want to replace the character with the new character "o" only at the 3rd position, we can't perform this operation because the replace function replaces both the  "j" characters; this is the major drawback of this operation.

Using translate( )

The translate( ) function updates the string by removing the character. The translate( ) removes characters based on the table value.

Syntax:

string.translate(table)

Example:

string= “Time”
a = “i”
b= “o”
table =string.maketrans(a, b)
print(stringtransulate(table))

Output:

Tome

Removing the first character from the string:

The first character in a string can be removed by using the following methods:

  • Removing the first character using the slicing technique.
  • Removing first character using the str.lstrip( )function.

Using slicing technique:

The first character in a string can be removed using the slicing technique-str[1:]. This str[1:] print the whole string except the first character.

Example:

s = “slice”
updated_string= s[1:]
     print (updated_string)

Output:

lice

Using str.lstrip( )  Method

The str.lstrip( ) method removes the first character from the string, and if the first character is repeated more than once, it will remove all the first character, which is repeated and prints the remaining string.

Example:

s = “SString”
updated_string = string.strip(“S”)
print (updated_string)

Output:

tring

Related Topics

Time. Sleep() in Python

Python time sleep () function suspends execution for a certain seconds given by user. Time sleep () syntax: Sleep(seconds) Limitations: The number of seconds to be suspends the code as per its requirements. Returns: Void The execution...

3 minutes read.

Python data science course

What is meant by Data Science? When processing raw, structured, and unstructured data utilizing various technologies, algorithms, and the scientific method, data science is a detailed study of the enormous quantity...

4 minutes read.

Python format() function

Python format() function The format() function in Python formats a specified value into the given format. A ‘TypeErrorexception’ is raised if the method search reaches the object and the format_spec is non-empty, or if either the format_spec or the...

1 minute read.

Periodogram in Python

Python:  Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Excel to CSV in Python

Define MS Excel Microsoft Excel is an application provided by the Microsoft corporation which allows us to build graphs to build tables, and it is also used for the macro programming...

4 minutes read.

Python Uses

Python has advanced in recent years to rank among the programming languages that are most often used globally. It is utilized in everything, including machine learning, software testing, and website...

4 minutes read.

Python email utils

Python is a popular programming language in this growing world. There are many resources to learn python online without spending a single penny. In this article, we will talk about a...

4 minutes read.

Python Arithmetic Operators

An arithmetic operator is a mathematical operator that is used to operate on two operands. Based on the operator used, action is performed on the operands, and output is delivered. Following...

3 minutes read.

Problem-solving with algorithm and data structures using Python

What is problem-solving? There is no universal method for solving problems. It's frequently a special process that balances your immediate and long-term goals with your available resources. However, several models emphasise...

3 minutes read.

Python Program to check whether a given number is Armstrong or not

Program to check whether a given number is Armstrong or not A number is said to be an Armstrong if the sum of each digit's cube of a given number equals...

1 minute read.

Python Memory Management

In order to manage memory, Python uses a private heap that contains all of its data structures and objects. The Python memory manager is responsible for the internal management of...

11 minutes read.

Introduction to Scratch programming

Scratch programming is generally designed for children who may create digital stories, games, and animations using the computer language Scratch, which has the largest kid-focused community in the world. Scratch...

3 minutes read.

Python SciPy Library

Introduction SciPy, a logical library for Python is an open-source, BSD-authorized library for arithmetic, science, and design. The SciPy library relies upon NumPy, which gives advantageous and quick N-dimensional exhibit control....

6 minutes read.

Image to NumPy Arrays in Python

Image is a simple process of working model confined to machine learning, Python works with the image in the data format of width and height i.e. Images are excelled into...

3 minutes read.

CatPlot in Python

Python Seaborn Library Seaborn is a superb Python tool for displaying graphical statistics graphing. Seaborn provides different color schemes and attractive default styles to facilitate the creation of various statistics charts...

8 minutes read.

Python Os sep

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Argmin

Introduction The argmin function is defined as numpy.argmin(). This function returns the index of the minimum value or element from a Numpy array in a specific axis. An array is taken...

3 minutes read.

Python Percentage Sign

In Python, the percentage sign significantly completes two things. They are: It goes about as a Modulo administrator. It helps in string organizing. Allow us to see every one of them plainly. Modulo operator: Like...

2 minutes read.

Matrix List Comprehension in Python

Introduction One of Python's most beautiful features is list comprehension. Iterating over an iterable object to create lists is a clever and succinct method. Nested List or matrix list Comprehensions, which...

6 minutes read.

How to import pandas in python

How to Install Pandas in Python Python is a vast ocean of libraries, modules, and different functions. It has a solution for almost everything. Using Python, we can simplify even a...

3 minutes read.