×

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

Sys Module in Python

What are Modules? The Modules are the kind of files that contain Python statements and definitions. The module is known by the name of the file followed by the suffix “.py”....

5 minutes read.

Subprocess in Python

In this tutorial, we will understand what is a subprocess in python and will understand how to use it. Subprocess A subprocess is a very prevailing portion of the python library. It...

3 minutes read.

Python JSON Schema

Python-jsonschema JSON: JSON stands for JavaScript Object Notation. It is a text-based format that represents structured data and can be used to interchange data among various applications. This is self-defining language...

5 minutes read.

Google Python Class

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 and its advantages

What is Python? Python is an object-oriented programming language, a general-purpose programming language with a high level, and also an interpreted language that has an easy syntax and dynamic semantics. Python...

7 minutes read.

Exclusive OR in Python

In Python, the exclusive OR (XOR) operator is represented by the caret symbol (^). It compares each bit of the first operand to the corresponding bit of the second operand,...

3 minutes read.

How to Practice Python Programming

Learning python kkis a step towards coding. Python gets one closer to programming languages. It is essential to practice every programming language to become a professional in coding. It is...

4 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.

Python Dictionary fromkeys() method

Python Dictionary fromkeys() method The dictionary.fromkeys() method in Python creates a new dictionary from the given sequence of elements and returns a dictionary with the specified keys and values. Syntax dictionary.fromkeys(sequence[, value]) Parameter sequence- This...

1 minute read.

List Comprehension in Python3

List A list in python is a complex data type, and a list is a collection of the number of data. The data variable may or may not be of the...

5 minutes read.

Python Filter List

To filter a list, we use filter () method function. The filter () will test every element true or not in the sequence. Syntax: Filter(function, sequence) Parameters: Function: Function that check and verifies if...

2 minutes read.

Python program to print calendar

Python program to print the calendar This article will explain how to print the calendar of month and year using Python's calendar module. It's a straightforward thing in Python by importing...

1 minute read.

Python MySQL Client

MySQL client is a project that is the subdivision of the MySQLdb package. This package provides an interface that runs the Python database API. Installing mysqlclient You can easily install mysqlclient with...

5 minutes read.

What is Python online compiler?

The compiler is a program that is used to scan an entire high-level program (source code) and it translates the scanned program into machine code. Compilers convert .py source code into...

5 minutes read.

Check if a String is Empty in Python

The string is one of the data types of Python. This data type stores all kinds of data but all the characters must be enclosed using double quotes (""). In...

5 minutes read.

_dict_ in Python

An unordered collection of data values known as a dictionary can be used in Python to store data values similar to a map. Dictionaries can also store a key: value...

6 minutes read.

Python Interface

When creating an application, it is important to continuously keep track of its changes. As an application grows, sometimes it gets hard to manage its updates and changes. Often, you...

4 minutes read.

Python Linked List

Linked List Linked lists non-contiguous linear data structure which is made up of nodes and used to store value and a pointer pointing to the next node. It is linked with...

6 minutes read.

Python System Requirements

Introduction As we know, Python is a popular programming language usually used to write scripts for operating systems. It’s handy adequate for utilizing in web development and application design. In this article,...

2 minutes read.

Linear Regression using Sklearn with Example

This article will examine Python's global, local and non-local variables and show you how to use them to write code without problems. Let's quickly review what a variable in Python is...

8 minutes read.