Strings in Python
Published: 2023-05-11 04:33:16


* The Purpose of this paper is to get required idea of methods of String in python in simplified way

* String is a collection of characters.

* Strings are immutable in python that means we can not change values of string but to create new string.

* we can access each character by their index

  
  

### __Methods:__

  

* ### __string.strip()__

	This method will delete the trailing and leading spaces of string

  
	
	```

		string = " yaswanth " #Notice there are spaces before and after the string

		name = string.strip()

		print(name) #output : 'yaswanth'

 * ### __string.split()__

	This method is overloaded, if we don't give any arguments to split() it will split the string at spaces and returns the list of strings.

	if we give argument to it, it will split the string at that character and returns list of strings
	
	```

			string = " hellow world "

			ls = string.split()

			print(ls) #output ['hellow','world']


			string = string.strip()

			ls = string.split('o)

			print(ls) #output ['hell','w w','rld']


  

* ### __string.lower()__

  
	
	It will convert the every alphabet in string to lower case



				string = 'YASWANTH'

				lower_case = string.lower()

				print(lower_case) #output 'yaswanth'



* ### __string.upper()__
	
	  

	It will convert the every alphabet in string to upper case

	  
	
	```
			string = 'yaswanth'

			upper_case = string.upper()

			print(lower_case) #output 'YASWANTH'

	```

	  

* ### __string.isdigit()__

  
	
	It will return a boolean value, if entire string is number returns true, else returns false.

	  

	```
			string = '12345'

			print(string.isdigit()) #prints True

			  

			string = ' 12345 ' #Notice there a space in front and back of string

			  

			print(string.isdigit()) #prints False

	```

  

* ### __string.isupper()__

	This will return true if all the alphabets of string are upper case else it will return false

	```

			string = 'HELLO WORLD'

			print(string.isupper()) #prints TRUE

			  

			string = 'Hello world'

			print(string.isupper()) #prints FALSE  

	```

  

* ### __string.islower()__

	This will return true if all the alphabets of string are lower case else it will return false

	```
		
			string = 'HELLO WORLD'

			print(string.isupper()) #prints FALSE

			  

			string = 'hello world'

			print(string.isupper()) #prints TRUE

* ### __string.join(iterable)__

  

	This function takes the iterable as a argument

	  

	This will join string to each item in the iterable and returns string

	```

			string = 'hello'

			  

			string = ' '.join(string)

			  

			print(string) # prints 'h e l l o ' as output
	```

  

* ### __string.startswith('object')__
	
	  

	This function returns True if string starts with given object else returns False

	  

	```
		
			string = 'hello, I am Yaswanth'

			print(string.startswith('hello')) #returns True
			
* ### __string.index('object')__
	
	This function returns the start index of object in string

	  

	```
			string = 'hey! There'	  
			print(string.index('There')) #returns 5
	```

	  

* ### __string.isalpha()__

  

	This function retuns boolean value. it will return True if all characters in string are alphabets else returns false.

	  

	```
			string = 'hello'

			print(string.isalpha()) #returns True
			
			string = '1 hello 2'

			print(string.isalpha()) #retuns False

	```

	  

  

* ### __string.format(arg1,arg2..)__
	
	This function is used to format a string by sending arguments to string which are not necessorly strings.

	Although we have string literal to do this work now it's always good to know legacy code

	  

	```
		
			name = 'yaswanth'

			age = 21

			print('my name is {}, my age is {}'.format(name,age))

			# prints 'my name is yaswanth, my age is 22'

       ```
## References:
* [Python Docs](https://docs.python.org/3/tutorial/datastructures.html)
* [w3 Schools](https://www.w3schools.com/python/python_dictionaries.asp)