Python Strings

Python Strings: Strings in Python can be as a sequence of characters of a derived data type. Python Strings are immutable. It means if once a string is assigned, it cannot be changed. Many python methods like replace(), join (), or split() modify strings, but do not modify the original string. Then they will only create a copy of the strings which they modify and return to the caller.

Python String Literals

In Python, strings can be created with single quotes, double quotes, or triple quotes. when we use triple quotes, strings can reach several lines without using escape characters. Square brackets can be used to access elements of strings. Subsets of the Python strings can be taken using slice operator ([] and [:]) with indexes starting at 0. The plus (+) sign indicates the string concatenation operator and the asterisk (*) is repetition operator.

And in Python, individual characters of a String can be accessed by using the method of Indexing, to access a range of characters in the String, method of slicing is used. Slicing in a String is done by using a Slicing operator (colon). Indexing allows negative address references to access characters from the back of the String.



Example

my_string = 'Hello'
print(my_string)
my_string = "Hello"
print(my_string)
my_string = '''Hello'''
print(my_string)
my_string = """Hello, welcome tofreshersnow"""
print(my_string)

Output
Hello
Hello
Hello
Hello, welcome to freshersnow

Deleting/Updating from a String

In Python updating and deleting, characters are not allowed.

strip( )

The strip() method returns a copy of the string in which all chars have been stripped from the beginning and the end of the string (default whitespace characters).

Syntax: str.strip([chars]);

Example

a = "Hello, World!"
print(a.strip())#returns  
"Hello, World!"

Output: Hello, world!

len( )

len( ) method return the length of the string. This method takes a string as a parameter. And returns integer

Syntax: len(string)

Example

a="Hello,World!"
print(len(a))

Output: 13

lower( )

It’s a built-in method for string handling. And the lower() methods return the lowercased string from the given string. It converts all uppercase characters to lowercase. If no uppercase characters exist, it returns the original string.

Syntax: string.lower()

Example

a="Hello, World!"
print(a.lower())

Output: Hello, world!

islower( )

The islower() method is a built-in method for string handling. It returns true if all the characters in the string are uppercase(). Otherwise, it returns false.

Syntax: string.islower()

Example

Input : string = ‘freshersnow’
Output: True

Input : string = ‘FreshersNow’
Output: False

upper( )

It returns the string in upper case. It converts all lowercase characters to uppercase. If no lowercase characters exist, it returns the original string.

Syntax: string.upper()

Example

a="Hello,World!"
print(a.upper())

Output: HELLO, WORLD!

islower( )

The islower( ) method is a built-in function. And it returns true if all the characters in the string are lower case. Otherwise false.

Syntax: string.islower()

Example

Input : string = ‘freshersnow’
Output : True

Input : string = ‘FreshersNow’
Output : False

replace( )

It replaces a string with other strings.

Syntax: string.replace(old, new, count)

Example

a="Hello, World!" 
print(a.replace("H","J"))

Output: Jello, Horld!

split( )

It splits the strings into substrings if it finds the instance of the separator.

Syntax: str.split(separator, maxsplit)

Here, separator acts as a delimiter. In case if it is not provided then any whitespace is a separator. And the maxsplit tells us to determine the maximum number of times should be provided. If itis not provided then there will be no limit.

Example

a="Hello,World!"
print(a.split(","))

Output: [‘Hello’ , ‘World!’]

Command Line Strings

Python allows command line input. i.e. we are able to ask the user for input.

Example

demo_string_input.py
Print("enter your name")
x=input()
print("Hello, ", x)

save the file as demo_string_input.py, and load the command line:

C:\Users:\Your name>python demo_string_input.py 

Enter your name:
user enter the name  as “freshersnow ”
Then finally it prints as follows

Output
Hello, freshersnow