Scribbling

Python String Methods 본문

Computer Science/Python

Python String Methods

focalpoint 2022. 1. 18. 15:15

Refer to the website below for the detailed instructions.

https://www.w3schools.com/python/python_ref_string.asp

 

Python String Methods

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

1. len(str)

- returns the length of the string

 

2. min(str), max(str)

- returns the min or max alphabetical character

 

3. str1.count(str2, begin, end):

- returns how many str2 is in str1[begin:end]

* Note that end index is not included

 

4. str1.startswith(str2, begin, end):

- returns whether str1 starts with str2

 

5. str1.endswith(str2, begin, end):

- returns whether str1 ends with str2

 

6. str1.rfind(str2)

- returns the index of the starting index of str2 in str1

- search starts from the right

- if str2 not in str1 -> returns -1

 

7. str1.index(str2)

- returns the index of str2 in str1

- if str2 not in str1 -> raises ValueError

 

8. str1.rindex(str2)

- returns the index of the starting index of str2 in str1

- search starts from the right

- if str2 not in str1 -> raises ValueError

 

9. str1.isalnum():

- returns whether str1 and all characters are alpahnumeric

 

10. str1.isalph()

- returns whether str1 and all characters are alphabetic

 

11. str1.isdigit()

- returns whether str1 and characters are digits

https://www.w3schools.com/python/ref_string_isdigit.asp

 

Python String isdigit() Method

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

12. str1.isnumeric():

- returns whether str1 and characters are digits

- basically very similar to isdigit(), with some minor differences. check out the below link for details.

https://www.w3schools.com/python/ref_string_isnumeric.asp

 

Python String isnumeric() Method

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

13. str1.isdecimal():

- returns whether str1 and all characters are in [0-9]

 

14. str1.islower():

- returns whether str1 and allcharacters are in lowercase.

 

15. str1.isupper()

- returns whether str1 and allcharacters are in uppercase.

 

16. str1.lower()

- returns a string where all str1's uppercase letters are converted to lowercase ones.

 

17. str1.upper():

- returns a string where all str1's lowercase letters are converted to uppercase ones.

 

18. str1.swapcase()

- returns a string where all str1's lowercase letters are converted to uppercase ones and vice versa.

 

19. str1.istitle()

- returns whether str1 and str1 is properly "titlecased"

 

20. str1.title()

- returns a new string which is 'titlecased' form of str1

 

21. str1.capitalize()

- returns a new string str1 of which first character converted to uppercase

 

22. str1.lstrip(set of characters)

txt = ",,,,,ssaaww.....banana"
x = txt.lstrip(",.asw")
print(x)

txt ='               moce'
x = txt.lstrip()
print(x)

 

23. str1.rstirp()

- returns a new string after removing spaces to the right of str1

 

24. str1.strip()

- if no params, returns a new string after removing spaces to the left & right of str1

- if params, returns a new string after removing those characters of str1

 

25. str1.isspace()

-returns whether str1 only consists of spaces

 

26. str1.center(width)

- returns a centered string with the given width

 

27. str.split(seperator, maxsplit)

- split the str to a list

https://www.w3schools.com/python/ref_string_split.asp

 

Python String split() Method

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

x = 'a b c d e f g'
l = x.split(maxsplit=3)
print(l)

 

28. str.splitlines(keeplinebreaks=False)

- returns a list with splitted string by line breakers

 

29. str.replace(oldvalue, newvalue, count)

https://www.w3schools.com/python/ref_string_replace.asp

 

Python String replace() Method

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

30. str.join(iterable)

https://www.w3schools.com/python/ref_string_join.asp

 

Python String join() Method

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

31. str.zfill(len)

- adds zeros to the start of the string until the given length

 

32. str.ljust(length, character)

- left justifies the string and add characters until the given length

 

33. str.rjust(length, character)

- right justifes the string and add characters until the given length

txt = "banana"

x = txt.rjust(20, 'x')

print(x, "is my favorite fruit.")

 

34. str.format(val1, val2, val3...)

https://www.w3schools.com/python/ref_string_format.asp

 

Python String format() Method

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

 

'Computer Science > Python' 카테고리의 다른 글

Python Magic Methods: Pythonic Data Model  (0) 2022.03.16
Python Grammar: Things that I forget often  (0) 2022.03.14
Python Int Representation & Bit Operations  (0) 2022.01.13
<Python> 정규표현식  (0) 2021.11.09
<Python> Decorator  (0) 2021.11.09