String in Python | Function related to string in Python

Initializing string in Python

X = "some string"
            or
X = 'some string'

print(x)


Getting character at a specified position

X = "some string"

Y = X[0]  #position in big brackets 1 st character is treated as 0th position

print(Y)


For getting character from back we have to mention -ve sign

z = X[-1]

print(z)


Getting a set of character from string

For getting a set of  a character from index a to index b we have to use

X= "some string"

Y = X[a:b] #b will be excluded

Y = X[5:8]


Getting length of string

X = "some string"

Y = len(X)

print(y)


Converting text into lower case or upper case

using x.lower()

X = "SOME string"

y=x.lower()

print(y)



Using x.upper

X = "some string"

Y = x.upper

print(y)

Removing space from start or end of a string

Using strip

X = "   some string  "

Y= x.strip()

print(Y)




Replacing a character with another

X = "hello world"
Y = X.replace("h","J")
print(Y)



Can you convert "hello world" into "helloworld" using replace

Spliting a string into substring

Using x.split()

Using split a string is converted into sub-string when a specified character is reached

X = "split, me"
Y = X.split(",")
print(Y)



Taking input from user

X = input()

note that input will take a string for taking integer input we have to use casting read about casting here

Y = int(X) #for getting integer input


Comments

Post a Comment