Python Forum
How do I delete symbols in a list of strings?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I delete symbols in a list of strings?
#1
I need help with my Python code? I need to test a string of values in the form of '####-####-####', for example; '5000-0000-0000'. I need to test this string values using four rules as stated in my code. However, this string contains the symbol; '-'. I need to remove the two dashes ('-') in the strings. How do I remove the two dashes? Here is my code that I've written so far. Let me know what you think:

def verify(number) :
  
  #Rule #1: first digit in credit number = 4
  first_digit = '4'
  if first_digit == number[0]:
    print('passes rule #1')
  else:
    print('violates rule #1')
  
  #Rule #2: the fourth digit must be +1 > fifth digit in the credit card number
  del number[4]
  del number[9]
  int(number)
  for num in number:
    if num[3]+1 > num[5]:
      print('passes rule #1 and #2')
    else:
      print('passes rule #1, violates rule #20')
  
  #Rule #3: the sum of all values in the credit card must be divisible by 4
  int(number) #convert number, which is a string of '####-####-####', to an integer
  sum = 0
  skip = False
  for i in number:
    if i == number[4] and number[5]:
      skip = True               #skip over the '-' symbol in the credit card value
      continue
    else:
      print('Error: cannot add '-' with integers')
    sum = sum + i
    if sum/4 == 0:
      print('passes rules #1-3')
    else:
      print('passes rule #1-2, violates rule #3')
    
  #Rule #4: Treat the first two digits as a two-digit number, and the seventh and eighth digits as a two-digit number, and their sum must be 100
  first_two_digit = number[0] + number[1]  #concatenate the two string values
  seventh_eighth_digit = number[7] + number[8] 
  int(first_two_digit)  #convert concatenated strings to integers
  int(seventh_eighth_digit)
  int(number)
  SumNum = first_two_digit + seventh_eighth_digit
  for num in number:
    if SumNum == 100:
      print('passes rules #1-4')
    else:
      print('passes rule #1-3, violates rule #4') 
    
  return verify

input = "5000-0000-0000" 
output = verify(input) 
print(output) 
the error I'm getting is the del number[4] and del number[9], because I can't delete the two dashes in the string list of '####-####-####'
Reply
#2
How about:
without_minus = number[0:4] + number[5:9] + number[10:]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read module/class from list of strings? popular_dog 1 424 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Python script that deletes symbols in plain text nzcan 3 641 Sep-05-2023, 04:03 PM
Last Post: deanhystad
  Trying to understand strings and lists of strings Konstantin23 2 697 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  problem in using int() with a list of strings akbarza 4 647 Jul-19-2023, 06:46 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  Help with Logical error processing List of strings dmc8300 3 1,030 Nov-27-2022, 04:10 PM
Last Post: Larz60+
  Splitting strings in list of strings jesse68 3 1,702 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Delete list while iterating shantanu97 1 1,866 Jun-06-2021, 11:59 AM
Last Post: Yoriz
  I cannot delete and the elements from the list quest 4 2,922 May-11-2021, 12:01 PM
Last Post: perfringo
  cyrillic symbols in tables in reportlab. hiroz 5 11,198 Sep-10-2020, 04:57 AM
Last Post: bradmalcom

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020