teslariu

metodos str

Dec 4th, 2021 (edited)
82
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Microsoft Windows [Versión 10.0.19042.1348]
  2. (c) Microsoft Corporation. Todos los derechos reservados.
  3.  
  4. C:\Users\Usuario>python
  5. Python 3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
  6. Type "help", "copyright", "credits" or "license" for more information.
  7. >>> s = "Hola a todos"
  8. >>> s[0]
  9. 'H'
  10. >>> s[0] = "h"
  11. Traceback (most recent call last):
  12. File "<stdin>", line 1, in <module>
  13. TypeError: 'str' object does not support item assignment
  14. >>> s[:]
  15. 'Hola a todos'
  16. >>> s[1:]
  17. 'ola a todos'
  18. >>> s[1:-1]
  19. 'ola a todo'
  20. >>> s[1:5]
  21. 'ola '
  22. >>> lista = [1,2,3,4,5,6,7,8,9,10]
  23. >>> lista[::2]
  24. [1, 3, 5, 7, 9]
  25. >>> lista[1::2]
  26. [2, 4, 6, 8, 10]
  27. >>> lista[2::3]
  28. [3, 6, 9]
  29. >>> lista[2:-1:3]
  30. [3, 6, 9]
  31. >>> s
  32. 'Hola a todos'
  33. >>> s[1:-1]
  34. 'ola a todo'
  35. >>> s[1:-1:2]
  36. 'oaatd'
  37. >>> s.startswith("H")
  38. True
  39. >>> s
  40. 'Hola a todos'
  41. >>> s.startswith("Hol")
  42. True
  43. >>> s.endswith("Hol")
  44. False
  45. >>> s.endswith("dos")
  46. True
  47. >>> s.endswith("todos")
  48. True
  49. >>> "a to" in s
  50. True
  51. >>> fila = "\tHola a todos \n"
  52. >>> fila
  53. '\tHola a todos \n'
  54. >>> print(fila)
  55. Hola a todos
  56.  
  57. >>> fila.strip()
  58. 'Hola a todos'
  59. >>> fila = " Hola a todos "
  60. >>> fila.strip()
  61. 'Hola a todos'
  62. >>> fila = "\tHola a todos \n"
  63. >>> fila.rstrip()
  64. '\tHola a todos'
  65. >>> fila.lstrip()
  66. 'Hola a todos \n'
  67. >>> s
  68. 'Hola a todos'
  69. >>> s.find("a")
  70. 3
  71. >>> s.index("a")
  72. 3
  73. >>> s.find("x")
  74. -1
  75. >>> s.index("x")
  76. Traceback (most recent call last):
  77. File "<stdin>", line 1, in <module>
  78. ValueError: substring not found
  79. >>> s.find("la")
  80. 2
  81. >>> s.find("a")
  82. 3
  83. >>> s.rfind("a")
  84. 5
  85. >>> s.rindex("a")
  86. 5
  87. >>> s.replace("Hola","Chau")
  88. 'Chau a todos'
  89. >>> s
  90. 'Hola a todos'
  91. >>> s = s.replace("Hola","Chau")
  92. >>> s
  93. 'Chau a todos'
  94. >>> s.replace("o","x")
  95. 'Chau a txdxs'
  96. >>> s.replace(" ","")
  97. 'Chauatodos'
  98. >>> s.replace("a","")
  99. 'Chu todos'
  100. >>> s
  101. 'Chau a todos'
  102. >>> s.lower()
  103. 'chau a todos'
  104. >>> s.upper()
  105. 'CHAU A TODOS'
  106. >>> s.swapcase()
  107. 'cHAU A TODOS'
  108. >>> s.casefold()
  109. 'chau a todos'
  110. >>> correo = "tatuss@ciudad.com.ar"
  111. >>> correo.split("@")
  112. ['tatuss', 'ciudad.com.ar']
  113. >>> nombre, server = correo.split("@")
  114. >>> nombre
  115. 'tatuss'
  116. >>> server
  117. 'ciudad.com.ar'
  118. >>> nombres = "Juan,Ale,Tito,Ana,Lucio"
  119. >>> nombres = nombres.split(",")
  120. >>> nombres
  121. ['Juan', 'Ale', 'Tito', 'Ana', 'Lucio']
  122. >>> lista = "root 1 2.5 1.0 /usr/bin/firefox"
  123. >>> usuario, pid, cpu, mem, proceso = lista.split(" ")
  124. >>> usuario
  125. 'root'
  126. >>> pid
  127. '1'
  128. >>> cpu
  129. '2.5'
  130. >>> mem
  131. '1.0'
  132. >>> proceso
  133. '/usr/bin/firefox'
  134. >>> correo.split("@")
  135. ['tatuss', 'ciudad.com.ar']
  136. >>> correo.partition("@")
  137. ('tatuss', '@', 'ciudad.com.ar')
  138. >>> "root 1 2.5 1.0 /usr/bin/firefox".split(" ")
  139. ['root', '1', '2.5', '1.0', '/usr/bin/firefox']
  140. >>> "root 1 2.5 1.0 /usr/bin/firefox".partition(" ")
  141. ('root', ' ', '1 2.5 1.0 /usr/bin/firefox')
  142. >>> "root 1 2.5 1.0 /usr/bin/firefox".rpartition(" ")
  143. ('root 1 2.5 1.0', ' ', '/usr/bin/firefox')
  144. >>> "root 1 2.5 1.0 /usr/bin/firefox".rpartition(" ")[-1]
  145. '/usr/bin/firefox'
  146. >>> "root 1 2.5 1.0 /usr/bin/firefox".partition(" ")[0]
  147. 'root'
  148. >>> usuario, pid, cpu, mem, proceso = lista.split(" ")
  149. >>> usuario
  150. 'root'
  151. >>> " ".join([usuario,pid,cpu,mem,proceso])
  152. 'root 1 2.5 1.0 /usr/bin/firefox'
  153. >>> nombre = "juan"
  154. >>> server = "gmail.com"
  155. >>> email = "@".join([nombre,server])
  156. >>> email
  157. 'juan@gmail.com'
  158. >>> nombres = ["juan","tito","ana"]
  159. >>> server
  160. 'gmail.com'
  161. >>> emails = []
  162. >>> for nombe in nombres:
  163. ... for nombe in nombres:
  164. KeyboardInterrupt
  165. >>> for nombre in nombres:
  166. ... emails.append("@".join([nombre,server]))
  167. ...
  168. >>> emails
  169. ['juan@gmail.com', 'tito@gmail.com', 'ana@gmail.com']
  170. >>> "34jkk!".isnumeric()
  171. False
  172. >>> "34jkk!".isascii()
  173. True
  174. >>> "34jkk!".isdigit()
  175. False
  176. >>> "34jkk!".alnum()
  177. Traceback (most recent call last):
  178. File "<stdin>", line 1, in <module>
  179. AttributeError: 'str' object has no attribute 'alnum'. Did you mean: 'isalnum'?
  180. >>> "34jkk!".isalnum()
  181. False
  182. >>> "34jkk!".isalpha()
  183. False
  184. >>> "234d".isdigit()
  185. False
  186. >>> "234d".isnumeric()
  187. False
  188. >>> "234d".isdecimal()
  189. False
  190. >>> "234.23".isdecimal()
  191. False
  192. >>> "234.23".isdigit()
  193. False
  194. >>> "234.23".isnumeric()
  195. False
  196. >>> "{:.2f}".format(23.58556)
  197. '23.59'
  198. >>> "{:.2f}".format(1,233,230.58556)
  199. '1.00'
  200. >>> "{:^10}".format(nombre)
  201. ' ana '
  202. >>> "{:<10}".format(nombre)
  203. 'ana '
  204. >>> "{:>10}".format(nombre)
  205. ' ana'
  206. >>> "{:e}".format(1254648648987498789789)
  207. '1.254649e+21'
  208. >>> "{:E}".format(1254648648987498789789)
  209. '1.254649E+21'
  210. >>>
  211. >>> s = "\t\nHola"
  212. >>> print(s)
  213.  
  214. Hola
  215. >>> s = "\t\nHola "
  216. >>> print(s)
  217.  
  218. Hola
  219. >>> print(repr(s))
  220. '\t\nHola '
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
RAW Paste Data Copied