Home >>Python Tutorial >Python String

Python String

The String Literals

It is important for you to remember that all kinds of string literals that are found in Python are always surrounded by quotation marks and those quotation marks could either be single quotation marks or single quotation marks.

This means that ‘hello’ would be treated similarly in this python tutorial with “hello”. We will be learning about the Python string methods in this part of the tutorial.

You as a developer should also be aware of the fact that you can use the print function to output all kinds of strings on the screen directly.

For example

print (“hello”)

The strings that are used in this programming language are basically some arrays of bytes that are used to represent a number of different Unicode characters.

This particular feature is not that unique and is quite similar to many other different programming languages. The only difference is in terms of the character data type as Python lacks this particular feature. This means that any particular single character is just a string that has a length of 1.

We will also be learning about Python string substitution. If any developer wishes to access the various elements of the string then he or she can use square brackets to do that.

The example for getting the character at position 1 when it is in position 0 is mentioned below.  

a = “Hello, World!”

print (a[1])

  Another example for the case of substring where you would want to get the character from the position 2 to position 5 is mentioned below.

b = “Hello, World!”

print (b[2:5])

  If you wish to remove the whitespace from the beginning or the end then you can choose to use the strip () method. For example

a = “ Hello, World ! ”

print (a.strip() ) # returns “Hello, World!”

  If you wish to return the length of the string then you should use the len() method. For example

a = “Hello, World!”

print (len (a) )

  If you wish to return the strings in the lower case then you should use the lower() method. For example

a = “Hello, World!”

print (a.lower() )

  To return the string in the upper case then you should use the upper() method. For example

a = “Hello, World!”

print (a.upper() )

  Now, we will be learning about the Python string replace method. And to replace one string with another kind of string you would have to use the replace() method. For example

a = “Hello, World!”

print (a.replace(“H” , “J”) )

  If you wish to split the string into substrings then you can use the split() method. However, to use this particular feature it needs to find some instances of the separator. The Python split example is mentioned below.

a = “Hello, World!”

print (a.split(“,”) ) # return [ ‘Hello’, ‘ World ! ’ ]

  The String Input for Command Line Python is a programming language that allows developers to input in the command line itself. This is one of the Python string methods. And this basically means that you are directly able to ask any particular user to input some kind of data or information.

This particular feature is illustrated by the example given below in which a user is asked to mention his or her name and once that name is entered it is then printed on the screen by using the input() method.

print (“Enter your name : ” )

x = input()

print (“Hello, “ + x)

  You can now save this file as demo_string_input.py and then you can load this file with the help of the command line in the following manner. C:\Users\Your Name>python demo_string_input.py After this step, the program will prompt the user for a string in the manner that is mentioned below.

Enter your name: The user can then enter his or her name: Linus And after all these steps, the program prints this information on the screen with a little message in the way that is mentioned below. Hello, Linus This is all for the Python string methods part of the Python tutorial.


No Sidebar ads